PHP-php 多维数组合并的问题
在这里举个小例子,大体就是这么个意思:
原来的数组是这样的:
'张三的购物单'=>array(
array(id => '送给李四的礼物',
name => '茶叶',
price => 50),
array(id => '送给王五的礼物',
name => '月饼'
price => 60),
array(id => '送给李四的礼物',
name => '茶叶蛋'
price => 20),
array(id => '送给王六的礼物',
name => '茶叶'
price => 50),
array(id => '送给王五的礼物',
name => '二锅头'
price => 100),
array(id => '送给李四的礼物',
name => '汤圆'
price => 60),
)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
上面的方法看着都好复杂,应该不用这么复杂吧
<?php
$example = array('张三的购物单'=>array(
array(id=>'送给李四的礼物',name => '茶叶',price => 50),
array(id => '送给王五的礼物',name => '月饼',price => 60),
array(id => '送给李四的礼物',name => '茶叶蛋',price => 20),
array(id => '送给王六的礼物',name => '茶叶',price => 50),
array(id => '送给王五的礼物',name => '二锅头',price => 100),
array(id => '送给李四的礼物',name => '汤圆',price => 60),
));
$result = array();
foreach($example as $key => $data) {
$result[$key] = array();
$tmp = & $result[$key];
foreach($data as $record) {
$id = & $record['id'];
// 初始化下数组
if(!isset($tmp[$id])) {
$tmp[$id] = array(
'name' => array(),
'price' => array(),
);
}
// 数组内容追加值
$tmp[$id]['name'][] = $record['name'];
$tmp[$id]['price'][] = $record['price'];
}
}
print_r($result);
更新:如果觉得上面的代码不能和题目一致的话,可以多加2行
<?php
$example = array('张三的购物单'=>array(
array(id=>'送给李四的礼物',name => '茶叶',price => 50),
array(id => '送给王五的礼物',name => '月饼',price => 60),
array(id => '送给李四的礼物',name => '茶叶蛋',price => 20),
array(id => '送给王六的礼物',name => '茶叶',price => 50),
array(id => '送给王五的礼物',name => '二锅头',price => 100),
array(id => '送给李四的礼物',name => '汤圆',price => 60),
));
$result = array();
foreach($example as $key => $data) {
$result[$key] = array();
$tmp = & $result[$key];
foreach($data as $record) {
$id = & $record['id'];
// 初始化下数组
if(!isset($tmp[$id])) {
$tmp[$id] = array(
'id' => $id, // 增加id
'name' => array(),
'price' => array(),
);
}
// 数组内容追加值
$tmp[$id]['name'][] = $record['name'];
$tmp[$id]['price'][] = $record['price'];
}
$result[$key] = array_values($result[$key]); // 消除id为key的结果
}
print_r($result);
$order=array(
array('id'=>'送给李四的礼物',
'name'=>'茶叶',
'price'=>50),
array('id'=>'送给王五的礼物',
'name'=>'月饼',
'price'=>60),
array('id'=>'送给李四的礼物',
'name'=>'茶叶蛋',
'price'=>50),
array('id'=>'送给王六的礼物',
'name'=>'茶叶',
'price'=>50),
array('id'=>'送给王五的礼物',
'name'=>'二锅头',
'price'=>100),
array('id'=>'送给李四的礼物',
'name'=>'汤圆',
'price'=>60
)
);
$arr =array();
foreach($order as $v)
{
if(array_key_exists($v['id'], $arr))
{
array_push($arr[$v['id']]['name'],$v['name']);
array_push($arr[$v['id']]['price'],$v['price']);
}
else
$arr[$v['id']]=array('id'=>$v['id'],'name'=>array($v['name']),'price'=>array($v['price']));
}
print_r($arr);
<?php
header('Content-Type:text/html;charset=utf-8');
$example = array('张三的购物单'=>array(
array(id=>'送给李四的礼物',name => '茶叶',price => 50),
array(id => '送给王五的礼物',name => '月饼',price => 60),
array(id => '送给李四的礼物',name => '茶叶蛋',price => 20),
array(id => '送给王六的礼物',name => '茶叶',price => 50),
array(id => '送给王五的礼物',name => '二锅头',price => 100),
array(id => '送给李四的礼物',name => '汤圆',price => 60),
));
$result = array();
foreach($example['张三的购物单'] as $source)
{
$temp = array();
$temp[name] = $source[name];
$temp[price] = $source[price];
$result[$source['id']][] = $temp;
}
print_r($result);
$response = array();
foreach($result as $key=>$value)
{
$temp = array();
foreach($value as $item)
{
$temp = array_merge_recursive($temp, $item);
}
$response[] = array(id=>$key, name=>$temp[name], price=>$temp[price]);
}
print_r($response);
思路就是把原来的数组整理成能用array_merge_recursive的格式,然后再重新整理下标。
$arr=array('张三的购物单'=>array(
array('id'=>'送给李四的礼物','name' => '茶叶','price' => 50),
array('id' => '送给王五的礼物','name' => '月饼','price' => 60),
array('id' => '送给李四的礼物','name' => '茶叶蛋','price' => 20),
array('id' => '送给王六的礼物','name' => '茶叶','price' => 50),
array('id' => '送给王五的礼物','name' => '二锅头','price' => 100),
array('id' => '送给李四的礼物','name' => '汤圆','price' => 60),
));
foreach ($arr['张三的购物单'] as $k=>$v){
$tmp[$v['id']]['id']=$v['id'];
isset($tmp[$v['id']]['name'])?$tmp[$v['id']]['name'] .=','.$v['name']:$tmp[$v['id']]['name'] .=$v['name'];
isset($tmp[$v['id']]['price'] )?$tmp[$v['id']]['price'] .=','.$v['price']:$tmp[$v['id']]['price'] .=$v['price'];
}
var_dump($tmp);
这样子也可以啊,多几个也行了
$A = array(
'张三的购物单'=>array(
array('id' => '送给李四的礼物','name' => '茶叶', 'price' => 50),
array('id' => '送给王五的礼物','name' => '月饼', 'price' => 60),
array('id' => '送给李四的礼物', 'name' => '茶叶蛋','price' => 20),
array('id' => '送给王六的礼物', 'name' => '茶叶','price' => 50),
array('id' => '送给王五的礼物', 'name' => '二锅头','price' => 100),
array('id' => '送给李四的礼物','name' => '汤圆','price' => 60),
),
'李四的购物单'=>array(
array('id' => '送给张三的礼物','name' => '茶叶', 'price' => 50),
array('id' => '送给王五的礼物','name' => '月饼', 'price' => 60),
array('id' => '送给张三的礼物', 'name' => '茶叶蛋','price' => 20),
array('id' => '送给王六的礼物', 'name' => '茶叶','price' => 50),
array('id' => '送给王五的礼物', 'name' => '二锅头','price' => 100),
array('id' => '送给张三的礼物','name' => '汤圆','price' => 60),
),
);
$B = array();
$C = array();
foreach($A as $key=>$value){
$i=0;
foreach($value as $akey=>$avalue){
if(empty($B)){
$B[$i]['id']=$avalue['id'];
$B[$i]['name']=$avalue['name'];
$B[$i]['price']=$avalue['price'];
$i++;
}else{
foreach($B as $bkey=>$bvalue){
if($bvalue['id']==$avalue['id']){
$B[$bkey]['name'] = $bvalue['name'].','.$avalue['name'];
$B[$bkey]['price'] = $bvalue['price'].','.$avalue['price'];
break;
}
if(($bkey+1)==count($B)){
$B[$i]['id']=$avalue['id'];
$B[$i]['name']=$avalue['name'];
$B[$i]['price']=$avalue['price'];
$i++;
}
}
}
}
$C[$key]=$B;
}
echo "<pre>";
var_dump($A);
var_dump($B);
var_dump($C);
exit;