php 多维数组合并的问题
2013-11-26 来自:Stand by... 9 人回应

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

  
'张三的购物单'=>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 => array('茶叶', '茶叶蛋', '汤圆'),
​ ​ ​ ​ ​ price => array(50, 20, 60),
​ ​ ​ ​ ​),
​ ​ ​ array(id => '送给王五的礼物',
​ ​ ​ ​ name => ​ array ( '月饼','二锅头'),
price => array(60, 100),
​ ​ ​ ),
​ ​ ​ array(id => '送给王六的礼物',
​ ​ ​ ​ ​name => '茶叶',
price => 50),
)

拜托大家指导一下,先谢谢了。

2014-03-27 来自:槐米

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

<?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);
2013-12-09 来自:胡子妹

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

$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;
2013-12-06 来自:Chloe.M
$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);
2013-12-04 来自:Responsibility
<?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的格式,然后再重新整理下标。

2013-12-02 来自:慢半拍

<?php
$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);
?>
</coding>

2013-12-01 来自:我们还未老
  
<?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);
2013-11-30 来自:ZRxin

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

  
$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;
2013-11-29 来自:ziweiyue

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

  
<?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);
2013-11-27 来自:导向性飞弹
  
$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);

您的回应

你还未登陆,不能回应!登陆