duanjihe5180 2017-01-12 09: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 10: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 没输出运行不了什么问题
  • ¥20 输入import torch显示Intel MKL FATAL ERROR,系统驱动1%,: Cannot load mkl_intel_thread.dll.
  • ¥15 点云密度大则包围盒小
  • ¥15 nginx使用nfs进行服务器的数据共享
  • ¥15 C#i编程中so-ir-192编码的字符集转码UTF8问题
  • ¥15 51嵌入式入门按键小项目
  • ¥30 海外项目,如何降低Google Map接口费用?
  • ¥15 fluentmeshing
  • ¥15 手机/平板的浏览器里如何实现类似荧光笔的效果
  • ¥15 盘古气象大模型调用(python)
手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部