테스트 사이트 - 개발 중인 베타 버전입니다

배열 질문입니다. 채택완료

Terrorboy 10년 전 조회 3,323

</span><span style="font-size: 10pt; line-height: 1.5;">​</span></p><p>array(</p><div><p style="margin-left: 40px;">[test1] => array</p><p style="margin-left: 80px;">(</p><p style="margin-left: 120px;">[0] => a1</p><p style="margin-left: 120px;">[1] => a2</p><p style="margin-left: 40px;"><span style="font-size: 10pt; line-height: 1.5;">)</span></p></div><p style="margin-left: 40px;">[test2] => array</p><p style="margin-left: 80px;">(</p><p style="margin-left: 120px;">[0] => b1</p><p style="margin-left: 120px;">[1] => b2</p><div><p style="margin-left: 40px;"><span style="font-size: 10pt; line-height: 1.5;">)</span></p><p>)</p><p><span style="font-size: 10pt; line-height: 1.5;"></span><span style="font-size: 10pt; line-height: 1.5;">

 

이런식의 배열이 있다면

 

다음과 같은 배열로 변경 하는 방법이 있을까요?

기초가 부족하다보니 for를 돌리고 있네요 ㅎㅎㅎ

</span></p><p><span style="font-size: 10pt; line-height: 1.5;">array (</span></p></div><p style="margin-left: 40px;"><span style="font-size: 10pt; line-height: 1.5;">[0] => array(</span></p><p style="margin-left: 80px;"><span style="font-size: 10pt; line-height: 1.5;">[test1]=>a1</span></p><p style="margin-left: 80px;"><span style="font-size: 10pt; line-height: 1.5;">[test2]=>b1</span></p><div style="margin-left: 40px;"><span style="font-size: 10pt; line-height: 1.5;">)</span></div><p style="margin-left: 40px;"><span style="font-size: 10pt; line-height: 1.5;">[1] => array(</span></p><p style="margin-left: 80px;"><span style="font-size: 10pt; line-height: 1.5;"></span></p><p style="margin-left: 80px;"><span style="font-size: 10pt; line-height: 1.5;">[test1]=>a2</span></p><p style="margin-left: 80px;"><span style="font-size: 10pt; line-height: 1.5;">[test2]=>b2</span><span style="font-size: 10pt; line-height: 1.5;"> </span></p><div><p style="margin-left: 40px;"><span style="font-size: 10pt; line-height: 1.5;">)</span></p><p><span style="font-size: 10pt; line-height: 1.5;">)</span></p><p><span style="font-size: 10pt; line-height: 1.5;">

 

 

 

 

목적

</span></p><p><span style="font-size: 10pt; line-height: 1.5;">[{'test1':'a1', 'test2': 'b1'}, </span><span style="font-size: 10pt; line-height: 1.5;">{'test1':'a2', 'test2': 'b2'}</span><span style="font-size: 10pt; line-height: 1.5;">​</span><span style="font-size: 10pt; line-height: 1.5;">]</span></p><p><span style="font-size: 10pt; line-height: 1.5;">

 처럼 요구하는 json처리를 위함

댓글을 작성하려면 로그인이 필요합니다.

답변 3개

채택된 답변
+20 포인트
10년 전

for 로 돌리셨으니 저는 foreach 로..;;  

 

</p><p>$s = array(</p><p>test1 => array(0 => 'a1',1 => 'a2'),
test2 => array(0 => 'b1',1 => 'b2')
);

print_r2($s);

$rr = array();
foreach($s as $k => $v){    
    $i=0;
    foreach($v as $j => $v2){
        $rr[$i][$k] = $v2;    
        $i++;
    }
}

print_r2($rr); </p><p>

 

 

로그인 후 평가할 수 있습니다

답변에 대한 댓글 3개

T
Terrorboy
10년 전
역시 방법은 똑같네요 ㅋㅋㅋ
일단 가장 빨리 근사 값에 도달 하셔서 채택!
뽁스
10년 전
책상 앞에 붙어 있는 php 배열 함수 찾아봐도 해결법(?)이 안 나와서 그냥 해본건데
채택을 해버리시니... 감사합니다~ㅎㅎㅎ

뭔가 더 좋은 방법이 있지 않을까 하며 다른 댓글을 기대하고 있었는데 이렇게 넘어가는군요~^^
T
Terrorboy
10년 전
네 더좋은 방법이 있을것 같은데 급한 마음에 짜버렸습니다 ㅠ.ㅠ
어제 몸상태가 극악이여서 머리가 정지된상태였거든요 ㅠ.ㅠ

댓글을 작성하려면 로그인이 필요합니다.

T
10년 전

</p><p>$Data = array('test1'=>array('a1', 'a2', 'a3'), 'test2'=>array('b1', 'b2', 'b3'));</p><p>$DataKay = array_keys($Data);</p><p><span style="font-size: 10pt; line-height: 1.5;">$Out = array();</span></p><p>foreach($Data as $key=>$val) {</p><p> </p><p><span class="Apple-tab-span" style="white-space:pre">	</span>for($i=0; $i<count($Data[$key]); $i++) {</p><p> </p><p><span class="Apple-tab-span" style="white-space:pre">		</span>$Out[$i][$key] = $Data[$key][$i];</p><p><span class="Apple-tab-span" style="white-space:pre">	</span>}</p><p>}</p><p>/*</p><p>결과:</p><p><span style="color: rgb(68, 68, 68); font-family: Tahoma, 굴림; font-size: 12px; line-height: 18px; background-color: rgb(255, 255, 255);">Array</span></p><span style="color: rgb(68, 68, 68); font-family: Tahoma, 굴림; font-size: 12px; line-height: 18px; background-color: rgb(255, 255, 255);">(</span><br style="color: rgb(68, 68, 68); font-family: Tahoma, 굴림; font-size: 12px; line-height: 18px; background-color: rgb(255, 255, 255);"><span style="color: rgb(68, 68, 68); font-family: Tahoma, 굴림; font-size: 12px; line-height: 18px; background-color: rgb(255, 255, 255);">    [0] => Array</span><br style="color: rgb(68, 68, 68); font-family: Tahoma, 굴림; font-size: 12px; line-height: 18px; background-color: rgb(255, 255, 255);"><span style="color: rgb(68, 68, 68); font-family: Tahoma, 굴림; font-size: 12px; line-height: 18px; background-color: rgb(255, 255, 255);">        (</span><br style="color: rgb(68, 68, 68); font-family: Tahoma, 굴림; font-size: 12px; line-height: 18px; background-color: rgb(255, 255, 255);"><span style="color: rgb(68, 68, 68); font-family: Tahoma, 굴림; font-size: 12px; line-height: 18px; background-color: rgb(255, 255, 255);">            [test1] => a1</span><br style="color: rgb(68, 68, 68); font-family: Tahoma, 굴림; font-size: 12px; line-height: 18px; background-color: rgb(255, 255, 255);"><span style="color: rgb(68, 68, 68); font-family: Tahoma, 굴림; font-size: 12px; line-height: 18px; background-color: rgb(255, 255, 255);">            [test2] => b1</span><br style="color: rgb(68, 68, 68); font-family: Tahoma, 굴림; font-size: 12px; line-height: 18px; background-color: rgb(255, 255, 255);"><span style="color: rgb(68, 68, 68); font-family: Tahoma, 굴림; font-size: 12px; line-height: 18px; background-color: rgb(255, 255, 255);">        )</span><br style="color: rgb(68, 68, 68); font-family: Tahoma, 굴림; font-size: 12px; line-height: 18px; background-color: rgb(255, 255, 255);"><br style="color: rgb(68, 68, 68); font-family: Tahoma, 굴림; font-size: 12px; line-height: 18px; background-color: rgb(255, 255, 255);"><span style="color: rgb(68, 68, 68); font-family: Tahoma, 굴림; font-size: 12px; line-height: 18px; background-color: rgb(255, 255, 255);">    [1] => Array</span><br style="color: rgb(68, 68, 68); font-family: Tahoma, 굴림; font-size: 12px; line-height: 18px; background-color: rgb(255, 255, 255);"><span style="color: rgb(68, 68, 68); font-family: Tahoma, 굴림; font-size: 12px; line-height: 18px; background-color: rgb(255, 255, 255);">        (</span><br style="color: rgb(68, 68, 68); font-family: Tahoma, 굴림; font-size: 12px; line-height: 18px; background-color: rgb(255, 255, 255);"><span style="color: rgb(68, 68, 68); font-family: Tahoma, 굴림; font-size: 12px; line-height: 18px; background-color: rgb(255, 255, 255);">            [test1] => a2</span><br style="color: rgb(68, 68, 68); font-family: Tahoma, 굴림; font-size: 12px; line-height: 18px; background-color: rgb(255, 255, 255);"><span style="color: rgb(68, 68, 68); font-family: Tahoma, 굴림; font-size: 12px; line-height: 18px; background-color: rgb(255, 255, 255);">            [test2] => b2</span><br style="color: rgb(68, 68, 68); font-family: Tahoma, 굴림; font-size: 12px; line-height: 18px; background-color: rgb(255, 255, 255);"><span style="color: rgb(68, 68, 68); font-family: Tahoma, 굴림; font-size: 12px; line-height: 18px; background-color: rgb(255, 255, 255);">        )</span><br style="color: rgb(68, 68, 68); font-family: Tahoma, 굴림; font-size: 12px; line-height: 18px; background-color: rgb(255, 255, 255);"><br style="color: rgb(68, 68, 68); font-family: Tahoma, 굴림; font-size: 12px; line-height: 18px; background-color: rgb(255, 255, 255);"><span style="color: rgb(68, 68, 68); font-family: Tahoma, 굴림; font-size: 12px; line-height: 18px; background-color: rgb(255, 255, 255);">    [2] => Array</span><br style="color: rgb(68, 68, 68); font-family: Tahoma, 굴림; font-size: 12px; line-height: 18px; background-color: rgb(255, 255, 255);"><span style="color: rgb(68, 68, 68); font-family: Tahoma, 굴림; font-size: 12px; line-height: 18px; background-color: rgb(255, 255, 255);">        (</span><br style="color: rgb(68, 68, 68); font-family: Tahoma, 굴림; font-size: 12px; line-height: 18px; background-color: rgb(255, 255, 255);"><span style="color: rgb(68, 68, 68); font-family: Tahoma, 굴림; font-size: 12px; line-height: 18px; background-color: rgb(255, 255, 255);">            [test1] => a3</span><br style="color: rgb(68, 68, 68); font-family: Tahoma, 굴림; font-size: 12px; line-height: 18px; background-color: rgb(255, 255, 255);"><span style="color: rgb(68, 68, 68); font-family: Tahoma, 굴림; font-size: 12px; line-height: 18px; background-color: rgb(255, 255, 255);">            [test2] => b3</span><br style="color: rgb(68, 68, 68); font-family: Tahoma, 굴림; font-size: 12px; line-height: 18px; background-color: rgb(255, 255, 255);"><span style="color: rgb(68, 68, 68); font-family: Tahoma, 굴림; font-size: 12px; line-height: 18px; background-color: rgb(255, 255, 255);">        )</span><br style="color: rgb(68, 68, 68); font-family: Tahoma, 굴림; font-size: 12px; line-height: 18px; background-color: rgb(255, 255, 255);"><br style="color: rgb(68, 68, 68); font-family: Tahoma, 굴림; font-size: 12px; line-height: 18px; background-color: rgb(255, 255, 255);"><p><span style="color: rgb(68, 68, 68); font-family: Tahoma, 굴림; font-size: 12px; line-height: 18px; background-color: rgb(255, 255, 255);">)</span> </p><p><span style="color: rgb(68, 68, 68); font-family: Tahoma, 굴림; font-size: 12px; line-height: 18px; background-color: rgb(255, 255, 255);">*/</span></p><p> </p><p>

로그인 후 평가할 수 있습니다

답변에 대한 댓글 1개

T
Terrorboy
10년 전
결국 오늘도 자답....

댓글을 작성하려면 로그인이 필요합니다.

10년 전

이런식은 어떨까요?

 

$prices = array(

    'Tires'=>100,

    'Oil'=>10,

    'Spark Plugs'=>4

);

 

while(list($product, $price) = each($prices)) {

    echo "$product - $price";

}

 

 

 

로그인 후 평가할 수 있습니다

답변에 대한 댓글 1개

T
Terrorboy
10년 전
범용으로 사용 하기 위하여 list는 제외 합니다 ㅎㅎㅎㅎ.... ㅠ.ㅠ;

댓글을 작성하려면 로그인이 필요합니다.

답변을 작성하려면 로그인이 필요합니다.

로그인