数组转换合并

WBOY
Release: 2016-06-23 13:51:03
Original
930 people have browsed it


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 (  Array( 'id' => 9,  'ti' => '我' ),  Array( 'id' => 10, 'ti' => '们' ),  Array( 'id' => 11, 'ti' => '中' ),  Array( 'id' => 11, 'ti' => '国' ));$res = array();foreach($a as $r) {  if(isset($res[$r['id']])) $res[$r['id']] .= $r['ti'];  else $res[$r['id']] = $r['ti'];}print_r($res);
Copy after login
Copy after login
Array(    [9] => 我    [10] => 们    [11] => 中国)
Copy after login
Copy after login

$a = Array (  Array( 'id' => 9,  'ti' => '我' ),  Array( 'id' => 10, 'ti' => '们' ),  Array( 'id' => 11, 'ti' => '中' ),  Array( 'id' => 11, 'ti' => '国' ));$res = array();foreach($a as $r) {  if(isset($res[$r['id']])) $res[$r['id']] .= $r['ti'];  else $res[$r['id']] = $r['ti'];}print_r($res);
Copy after login
Copy after login
Array(    [9] => 我    [10] => 们    [11] => 中国)
Copy after login
Copy after login


版主不好意思我还想请教$res[$r['id']] .= $r['ti'];中间.符号用来起什么作用的?如果把$res和下面的数组合并,遇到相同KEY替换他的值。能不能做到?
Array ( [7] => 爱[8] => 我 [9] => 11 [10] => 33333 [11] => 中 [12] => 华 )

哈哈,一直用array_merge()合并,原来直接用+号就行了。。。。

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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!