배열 질문입니다. 채택완료
</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개
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개
채택을 해버리시니... 감사합니다~ㅎㅎㅎ
뭔가 더 좋은 방법이 있지 않을까 하며 다른 댓글을 기대하고 있었는데 이렇게 넘어가는군요~^^
어제 몸상태가 극악이여서 머리가 정지된상태였거든요 ㅠ.ㅠ
댓글을 작성하려면 로그인이 필요합니다.
</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개
댓글을 작성하려면 로그인이 필요합니다.
답변을 작성하려면 로그인이 필요합니다.
로그인
일단 가장 빨리 근사 값에 도달 하셔서 채택!