Home  >  Article  >  Backend Development  >  数组转换归拢

数组转换归拢

WBOY
WBOYOriginal
2016-06-13 12:05:15914browse

数组转换合并

本帖最后由 u012984377 于 2014-08-22 15:09:59 编辑
Array ( [0] => Array ( [id] => 9 [ti] => 我 ) [1] => Array( [id] => 10 [ti] => 们 ) [2] => Array( [id] => 11 [ti] => 中 )[3] => Array( [id] => 11 [ti] => 国 ))
这个转换成一维数组ID做为KEY,转换我会,但遇到相同的就合并怎么做?
如果得到一个一维数组,把得到的一维数组与下面的合并,如果KEY相等就替换值;

Array ( [7] => 爱[8] => 我 [9] => 11 [10] => 33333 [11] => 中 [12] => 华 )

目前练习数组遇到的两个问题,请高手指导。搞了快一天,还是失败了。。。-_-
------解决方案--------------------
$a = Array (<br />  Array( 'id' => 9,  'ti' => '我' ),<br />  Array( 'id' => 10, 'ti' => '们' ),<br />  Array( 'id' => 11, 'ti' => '中' ),<br />  Array( 'id' => 11, 'ti' => '国' )<br />);<br />$res = array();<br />foreach($a as $r) {<br />  if(isset($res[$r['id']])) $res[$r['id']] .= $r['ti'];<br />  else $res[$r['id']] = $r['ti'];<br />}<br />print_r($res);
Array<br />(<br />    [9] => 我<br />    [10] => 们<br />    [11] => 中国<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