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

时间:2017-01-12 17:13:12

标签: php

我有一个像这样的简单二维数组:

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

我需要对idname进行分组,然后创建一个具有不同值的数组,如下所示:

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

使用PHP进行此操作的最佳方法是什么?

这是我的尝试:

<?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 个答案:

答案 0 :(得分:1)

使用id作为新数组的关键字,然后只需添加下一个companyprice

的新数组
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']);
}

如果你需要重新索引它(可能不是):

$result = array_values($result);