Home > Backend Development > PHP Tutorial > php Array数组转换问题

php Array数组转换问题

WBOY
Release: 2016-06-23 14:06:13
Original
1115 people have browsed it

如何把

Array(    [strwarenum] => Array        (            [0] => 1            [1] => 1        )    [strwareid] => Array        (            [0] => 80            [1] => 79        ))
Copy after login


转换为

Array(    [config_id] =>     [strwareid] => ,78,80    [strwarenum] => ,1,1    [config_name] =>     [config_desc] =>     [diycat_id] => )
Copy after login
Copy after login

并可以单独调用呢


回复讨论(解决方案)

设你原来的数组为$a,建一个你需要包含所有key的空数组$b(你的例子有些key在$a中没有,所以要另建)
不知道你的 78这个值从何而来,暂时当你是把79打错了
我懒得写赋值部分了,还要改你的代码,麻烦,自己写吧

foreach($b as $k => $v){    $a[$k][]='';    $v=array_reverse($a[$k]);}var_export($b);
Copy after login

自己测试

非常抱歉,实在不会把

Array(    [strwarenum] => Array        (            [80] => 1            [78] => 1        )    [strwareid] => Array        (            [0] => 80            [1] => 78        )    [config_id] =>     [config_name] =>     [config_desc] =>     [diycat_id] => )
Copy after login

变为
Array(    [config_id] =>     [strwareid] => ,78,80    [strwarenum] => ,1,1    [config_name] =>     [config_desc] =>     [diycat_id] => )
Copy after login
Copy after login

$arr=你的数组;foreach($arr as &$v){     if(is_array($v)) $v=','.join(',',$v);}print_r($arr);
Copy after login

PHP code?12345$arr=你的数组;foreach($arr as &$v){ if(is_array($v)) $v=','.join(',',$v);}print_r($arr);


非常感谢,解决了

	$ar=array("strwarenum"=>array(1,1),"strwareid"=>array(80,79));		function change($ar){		$arr=array();		foreach($ar as $key=>$value){			if($key=="strwareid"){				for($i=count($ar[$key])-1;$i>=0;$i--){					$s=$s.",".$ar[$key][$i];				}			}			if($key=="strwarenum"){				for($i=count($ar[$key])-1;$i>=0;$i--){					$m=$m.",".$ar[$key][$i];				}			}		}		$arr['config_id']='';		$arr['strwareid']=$s;		$arr['strwarenum']=$m;		$arr['config_name']='';		$arr['config_desc']='';		$arr['diycat_id']='';		return $arr;	}	echo "<pre class="brush:php;toolbar:false">";	print_r(change($ar));	echo "
Copy after login
";Array( [config_id] => [strwareid] => ,79,80 [strwarenum] => ,1,1 [config_name] => [config_desc] => [diycat_id] => )

呃,还有连带关系?strwarenum的key和strwareid的值对应么?给两个不同的strwarenum的值看看结果是怎样

另外,你给的数据能不能写成定义形式啊 "strwarenum" => Array...,还要我逐个去改啊?! 

居然结贴了……如果有对应关系,上面的各楼包括我写的那个都不能用

PHP code?123456789101112131415161718192021222324252627282930313233343536    $ar=array("strwarenum"=>array(1,1),"strwareid"=>array(80,79));         function change($ar){        $arr=array(……
其实strwarenum 是数量可以不考虑。对应每个strwareid为1也可以
strwareid是主要要获得的函数

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