Home > Backend Development > PHP Tutorial > 数组纵向转横向如何实现啊

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

WBOY
Release: 2016-06-23 13:52:43
Original
1047 people have browsed it

array(	6=>array(5=>'黑xx', 42=>'XXL'),	7=>array(5=>'黑xx', 40=>'XL'),	8=>array(8=>'灰',   40=>'XL'),);//结果:array(	0=>array(5=>'黑xx', 8=>'灰'),	1=>array(42=>'XXL', 40=>'XL'),);
Copy after login

数组纵向转横向并过滤重复值 是如何实现啊



回复讨论(解决方案)

$a = array(	6=>array(5=>'黑xx', 42=>'XXL'),	7=>array(5=>'黑xx', 40=>'XL'),	8=>array(8=>'灰',   40=>'XL'),);foreach($a as $r) {  $b[key($r)] = current($r);  next($r);  $c[key($r)] = current($r);}$b = array($b, $c);print_r($b);
Copy after login
Array(    [0] => Array        (            [5] => 黑xx            [8] => 灰        )    [1] => Array        (            [42] => XXL            [40] => XL        ))
Copy after login
Copy after login
Copy after login
Copy after login

$arr = array(    6=>array(5=>'黑xx', 42=>'XXL'),    7=>array(5=>'黑xx', 40=>'XL'),    8=>array(8=>'灰',   40=>'XL'),);$result = array();foreach($arr as $a){    $keys = array_keys($a);    $vals = array_values($a);    $result[0][$keys[0]] = $vals[0];    $result[1][$keys[1]] = $vals[1];}print_r($result);
Copy after login


Array(    [0] => Array        (            [5] => 黑xx            [8] => 灰        )    [1] => Array        (            [42] => XXL            [40] => XL        ))
Copy after login
Copy after login
Copy after login
Copy after login

最简单的写法

$a = array(    6=>array(5=>'黑xx', 42=>'XXL'),    7=>array(5=>'黑xx', 40=>'XL'),    8=>array(8=>'灰',   40=>'XL'),); foreach($a as $r) {  $i = 0;  while(list($k, $v) = each($r)) $b[$i++][$k] = $v;}print_r($b);
Copy after login
Copy after login
Array(    [0] => Array        (            [5] => 黑xx            [8] => 灰        )    [1] => Array        (            [42] => XXL            [40] => XL        ))
Copy after login
Copy after login
Copy after login
Copy after login

最简单的写法

$a = array(    6=>array(5=>'黑xx', 42=>'XXL'),    7=>array(5=>'黑xx', 40=>'XL'),    8=>array(8=>'灰',   40=>'XL'),); foreach($a as $r) {  $i = 0;  while(list($k, $v) = each($r)) $b[$i++][$k] = $v;}print_r($b);
Copy after login
Copy after login
Array(    [0] => Array        (            [5] => 黑xx            [8] => 灰        )    [1] => Array        (            [42] => XXL            [40] => XL        ))
Copy after login
Copy after login
Copy after login
Copy after login



这个方法好!!!!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template