duanjihe5180 2017-01-12 17:13 采纳率: 100%
浏览 10
已采纳

PHP组由重复值组成的多维数组

I have a simple two dimensional array like this:

Array
(
    [0] => Array
        (
            [id] => 1
            [name] => John
            [company] => One
            [price] => 12.22
        )

    [1] => Array
        (
            [id] => 1
            [name] => John
            [company] => Two
            [price] => 14.33
        )
    [2] => Array
        (
            [id] => 2
            [name] => Mike
            [company] => One
            [price] => 15.11
        )
    [3] => Array
        (
            [id] => 2
            [name] => Mike
            [company] => Two
            [price] => 10.12
        )

    [4] => Array
        (
            [id] => 3
            [name] => Paul
            [company] => One
            [price] => 42.22
        )
    [5] => Array
        (
            [id] => 3
            [name] => Paul
            [company] => Two
            [price] => 56.62
        )
    [6] => Array
        (
            [id] => 3
            [name] => Paul
            [company] => Three
            [price] => 16.12
        )
)

I need to group id and name, then create an array with different values something like this:

Array
(
    [0] => Array
        (
            [id] => 1
            [name] => John
            [companies] => array (
                                  array(
                                        [company] => One 
                                        [price] => 12.22
                                  )
                                  array(
                                        [company] => Two
                                        [price] => 14.33
                                  )
                           )               
        )

    [1] => Array
        (
            [id] => 2
            [name] => Mike
            [companies] => array (
                                  array(
                                        [company] => One 
                                        [price] => 15.11
                                  )
                                  array(
                                        [company] => Two
                                        [price] => 10.12
                                  )
                           )               
        )
    [2] => Array
        (
            [id] => 3
            [name] => Paul
            [companies] => array (
                                  array(
                                        [company] => One 
                                        [price] => 42.22
                                  )
                                  array(
                                        [company] => Two
                                        [price] => 56.62
                                  )
                                  array(
                                        [company] => Three
                                        [price] => 16.12
                                  )
                           )              
        )        
)

What is the best way to do it with PHP?

This is my attemp:

<?php
$items=array();
$temp = 0;
$companies = array('uno','dos','tres');
foreach ($values as $value) {

  if ($temp == $value['id'] )
    continue;                            
  else
    $temp == $value['id'];

  foreach ($companies as $key => $company){
    foreach ($values as $item){
      if ($item['id'] == $temp && $item['company'] == $key)
        $value['company'][$key] = $item['price'];
    }
  }

  $items[] = $value;

}
  • 写回答

1条回答 默认 最新

  • duanping1920 2017-01-12 18:52
    关注

    Use the id as the key for the new array, then just append a new array with the next company and price

    foreach($array as $v) {
        $result[$v['id']]['id']          = $v['id'];
        $result[$v['id']]['name']        = $v['name'];
        $result[$v['id']]['companies'][] = array('company' => $v['company'],
                                                 'price'   => $v['price']);
    }
    

    If you need to re-index it (probably not):

    $result = array_values($result);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥30 python代码,帮调试,帮帮忙吧
  • ¥15 #MATLAB仿真#车辆换道路径规划