Home >Backend Development >PHP Tutorial >php数组合并解决方案

php数组合并解决方案

WBOY
WBOYOriginal
2016-06-13 12:26:21965browse

php数组合并

<br />$a=Array<br />(<br />    [0] => Array<br />        (<br />            [newsupdate_id] => 1<br />        )<br />    [1] => Array<br />        (<br />            [newsupdate_id] => 2<br />        )<br />}<br />  <br />$b=Array<br />(<br />    [0] => 张三<br />    [1] => 李四<br />}<br />  <br />想要的结果<br />$c=Array<br />(<br />    [0] => Array<br />        (<br />            [newsupdate_id] => 1<br />            [newsupdate_name] => 张三<br />        )<br />    [1] => Array<br />        (<br />            [newsupdate_id] => 2<br />            [newsupdate_name] => 李四<br />        )<br />}<br />

------解决思路----------------------
这样更一般
$a = array(<br />  array('newsupdate_id' => 1),<br />  array('newsupdate_id' => 2),<br />);<br />   <br />$b = array('张三', '李四');<br /><br />foreach($a as $i=>$v) {<br />  $c[] = array_merge($v, array('newsupdate_name' => $b[$i]));<br />}<br />print_r($c);
Array<br />(<br />    [0] => Array<br />        (<br />            [newsupdate_id] => 1<br />            [newsupdate_name] => 张三<br />        )<br /><br />    [1] => Array<br />        (<br />            [newsupdate_id] => 2<br />            [newsupdate_name] => 李四<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