php 2个二维数组合成一个2维数组,条件是lid=id的合并?
Array([0]=>Array([lid]=>67[title]=>T恤)[1]=>Array([lid]=>27[title]=>衬衣))Array([0]=>Arr...
Array(
[0] => Array(
[lid] => 67
[title] => T恤
)
[1] => Array(
[lid] => 27
[title] => 衬衣
)
)
Array(
[0] => Array(
[id] => 67
[url] => baidu.com
)
[1] => Array(
[id] => 27
[url] => jd.com
)
)
求大神解 展开
[0] => Array(
[lid] => 67
[title] => T恤
)
[1] => Array(
[lid] => 27
[title] => 衬衣
)
)
Array(
[0] => Array(
[id] => 67
[url] => baidu.com
)
[1] => Array(
[id] => 27
[url] => jd.com
)
)
求大神解 展开
2个回答
展开全部
按照你的要求合并两个二维数组的PHP程序如下
<?php
$arr1 =array(array('lid' => 67, 'title' => 'T恤'), array('lid' => 27, 'title' => '衬衣'));
$arr2 =array(array('id' => 67, 'url' => 'baidu.com'), array('id' => 27, 'url' => 'jd.com'));
$result=array();
for($i=0;$i<count($arr1);$i++){
$result[$i]=$arr1[$i];
for($j=0;$j<count($arr2);$j++){
if ($arr1[$i]['lid']==$arr2[$j]['id']){
$result[$i]['url'] = $arr2[$j]['url'];
}
}
}
print_r($result);
?>
展开全部
$arr1 = array(array('lid' => 67, 'title' => 'T恤'), array('lid' => 27, 'title' => '衬衣'));
$arr2 = array(array('id' => 67, 'url' => 'baidu.com'), array('id' => 27, 'url' => 'jd.com'));
$data = [];
foreach($arr1 as $value1)
{
$lid = $value1['lid'];
foreach($arr2 as $value2)
{
if($lid == $value2['id'])
{
$data[] = array_merge($value1, $value2);
break;
}
}
}
var_dump($data);
$arr2 = array(array('id' => 67, 'url' => 'baidu.com'), array('id' => 27, 'url' => 'jd.com'));
$data = [];
foreach($arr1 as $value1)
{
$lid = $value1['lid'];
foreach($arr2 as $value2)
{
if($lid == $value2['id'])
{
$data[] = array_merge($value1, $value2);
break;
}
}
}
var_dump($data);
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询