Home >Backend Development >PHP Tutorial >php 合并两个数组并且键值一样的在一块。

php 合并两个数组并且键值一样的在一块。

WBOY
WBOYOriginal
2016-06-06 20:33:431448browse

$a = array(

<code>0=>array('pid'=>'0001'),
1=>array('pid'=>'0002'),
2=>array('pid'=>'0004'),
3=>array('pid'=>'0006'),
4=>array('pid'=>'0008'),
</code>

);

$b = array(

<code>0=>array('pid'=>'0001'),
1=>array('pid'=>'0002'),
2=>array('pid'=>'0003'),
3=>array('pid'=>'0004'),
4=>array('pid'=>'0004'),
5=>array('pid'=>'0005'),
6=>array('pid'=>'0007'),
</code>

);直接把$a的元素插入$b里边,并且键值一样的在一起。尽量不用重新排序的方式.什么sort,array_multisort之类的。
$ab=array(
0=>array('pid'=>'0001'),
1=>array('pid'=>'0001'),
2=>array('pid'=>'0002'),
3=>array('pid'=>'0002'),
4=>array('pid'=>'0003'),
5=>array('pid'=>'0004'),
6=>array('pid'=>'0004'),
7=>array('pid'=>'0004'),
8=>array('pid'=>'0005'),
9=>array('pid'=>'0006'),
10=>array('pid'=>'0007'),
11=>array('pid'=>'0008'),
)

回复内容:

$a = array(

<code>0=>array('pid'=>'0001'),
1=>array('pid'=>'0002'),
2=>array('pid'=>'0004'),
3=>array('pid'=>'0006'),
4=>array('pid'=>'0008'),
</code>

);

$b = array(

<code>0=>array('pid'=>'0001'),
1=>array('pid'=>'0002'),
2=>array('pid'=>'0003'),
3=>array('pid'=>'0004'),
4=>array('pid'=>'0004'),
5=>array('pid'=>'0005'),
6=>array('pid'=>'0007'),
</code>

);直接把$a的元素插入$b里边,并且键值一样的在一起。尽量不用重新排序的方式.什么sort,array_multisort之类的。
$ab=array(
0=>array('pid'=>'0001'),
1=>array('pid'=>'0001'),
2=>array('pid'=>'0002'),
3=>array('pid'=>'0002'),
4=>array('pid'=>'0003'),
5=>array('pid'=>'0004'),
6=>array('pid'=>'0004'),
7=>array('pid'=>'0004'),
8=>array('pid'=>'0005'),
9=>array('pid'=>'0006'),
10=>array('pid'=>'0007'),
11=>array('pid'=>'0008'),
)

<code>php</code><code><br>$c = array_merge($a,$b);

foreach ($c as $key => $value) {
  $data[] = $value['pid'];
}
array_multisort($c,SORT_ASC ,  $data );//不用array_multisort?
 print_r($c);
</code>
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