Home >Backend Development >PHP Tutorial >数组纵向转横向怎么实现

数组纵向转横向怎么实现

WBOY
WBOYOriginal
2016-06-13 12:03:411296browse

数组纵向转横向如何实现啊

<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 />

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn