Home > Backend Development > PHP Tutorial > 如何将一个多维数组转换成一维

如何将一个多维数组转换成一维

WBOY
Release: 2016-06-23 13:51:10
Original
858 people have browsed it


多维数组打印来出效果:
Array ( [10] => 准备买 [11] => 不准备换 [12] =>  Array ( [0] => 3厢小轿车 [1] => 城市SUB ) [13] => 33333 [14] =>  Array ( [0] => 中型 [1] => 小巧型 ) [15] => 全险 )
想转换成一维的,保持原来KEY不变,遇到多维就合并,最终想得到
Array ( [10] => 准备买 [11] => 不准备换  [12] =>  3厢小轿车, 城市SUB  [13] => 33333  [14] =>  中型 ,小巧型  [15] => 全险 )

搞了半天,有点迷糊了。请高手看看


回复讨论(解决方案)

$a = Array (  10 => '准备买',  11 => '不准备换',  12 => Array ( 0 => '3厢小轿车', 1 => '城市SUB' ),  13 => 33333,  14 => Array ( 0 => '中型', 1 => '小巧型' ),  15 => '全险',);foreach($a as $k=>$v) {  if(is_array($v)) $a[$k] = join(',', $v);}print_r($a);
Copy after login
Array(    [10] => 准备买    [11] => 不准备换    [12] => 3厢小轿车,城市SUB    [13] => 33333    [14] => 中型,小巧型    [15] => 全险)
Copy after login
Copy after login

楼上是正解。

$a = array (  10 => '准备买',  11 => '不准备换',  12 => Array ( 0 => '3厢小轿车', 1 => '城市SUB' ),  13 => 33333,  14 => Array ( 0 => '中型', 1 => '小巧型' ),  15 => '全险',);$b=array();foreach($a as $k=>$v){    $b[$k]=$v;    if(is_array($v)){        $b[$k]=$v[0].','.$v[1];    }}print_r($b);
Copy after login


Array(    [10] => 准备买    [11] => 不准备换    [12] => 3厢小轿车,城市SUB    [13] => 33333    [14] => 中型,小巧型    [15] => 全险)
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