PHP-php 多维数组合并的问题

发布于 2017-01-04 18:25:28 字数 780 浏览 1294 评论 5

在这里举个小例子,大体就是这么个意思:
原来的数组是这样的:

'张三的购物单'=>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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

甜柠檬 2017-10-11 16:13:21

上面的方法看着都好复杂,应该不用这么复杂吧

<?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);

清晨说ぺ晚安 2017-06-14 11:32:22

  $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);

想挽留 2017-04-01 08:20:54

 <?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的格式,然后再重新整理下标。

灵芸 2017-03-15 18:44:34

$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);

夜无邪 2017-01-29 07:50:35

这样子也可以啊,多几个也行了

 $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;

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文