Heim >Backend-Entwicklung >PHP-Tutorial >数组纵向转横向怎么实现
数组纵向转横向如何实现啊
<br />array(<br /> 6=>array(5=>'黑xx', 42=>'XXL'),<br /> 7=>array(5=>'黑xx', 40=>'XL'),<br /> 8=>array(8=>'灰', 40=>'XL'),<br />);<br /><br />//结果:<br />array(<br /> 0=>array(5=>'黑xx', 8=>'灰'),<br /> 1=>array(42=>'XXL', 40=>'XL'),<br />);<br /><br />
<br />$arr = array(<br /> 6=>array(5=>'黑xx', 42=>'XXL'),<br /> 7=>array(5=>'黑xx', 40=>'XL'),<br /> 8=>array(8=>'灰', 40=>'XL'),<br />);<br /><br />$result = array();<br /><br />foreach($arr as $a){<br /> $keys = array_keys($a);<br /> $vals = array_values($a);<br /> $result[0][$keys[0]] = $vals[0];<br /> $result[1][$keys[1]] = $vals[1];<br />}<br /><br />print_r($result);<br />
<br />Array<br />(<br /> [0] => Array<br /> (<br /> [5] => 黑xx<br /> [8] => 灰<br /> )<br /><br /> [1] => Array<br /> (<br /> [42] => XXL<br /> [40] => XL<br /> )<br /><br />)<br />
$a = array(<br /> 6=>array(5=>'黑xx', 42=>'XXL'),<br /> 7=>array(5=>'黑xx', 40=>'XL'),<br /> 8=>array(8=>'灰', 40=>'XL'),<br />);<br /> <br />foreach($a as $r) {<br /> $i = 0;<br /> while(list($k, $v) = each($r)) $b[$i++][$k] = $v;<br />}<br />print_r($b);
Array<br />(<br /> [0] => Array<br /> (<br /> [5] => 黑xx<br /> [8] => 灰<br /> )<br /><br /> [1] => Array<br /> (<br /> [42] => XXL<br /> [40] => XL<br /> )<br /><br />)<br /><br />