php - 按列对二维数组进行分组,保留多个其他列值并将 2 列值插入子数组

标签 php arrays multidimensional-array grouping sub-array

我有一个包含重复值的数组,如下所示:

[
    [
        'id' => 112,
        'name' => 'John',
        'country' => 'Spain',
        'age' => 24,
        'company' => 'One',
        'price' => 10
    ],
    [
        'id' => 112,
        'name' => 'John',
        'country' => 'Spain',
        'age' => 24,
        'company' => 'Two',
        'price' => 15
    ],
    [
        'id' => 112,
        'name' => 'John',
        'country' => 'Spain',
        'age' => 24,
        'company' => 'Three',
        'price' => 20
    ],
    [
        'id' => 224,
        'name' => 'Paul',
        'country' => 'France',
        'age' => 25,
        'company' => 'One',
        'price' => 25
    ],
    [
        'id' => 224,
        'name' => 'Paul',
        'country' => 'France',
        'age' => 25,
        'company' => 'Two',
        'price' => 40
    ]
]

我需要将相同的idnamecountryage分组,并创建一个“子数组” "company 包含 companyprice

array (
  112 => 
  array (
    'id' => 112,
    'name' => 'John',
    'country' => 'Spain',
    'age' => 24,
    'companies' => 
    array (
      0 => 
      array (
        'company' => 'One',
        'price' => 10,
      ),
      1 => 
      array (
        'company' => 'Two',
        'price' => 15,
      ),
      2 => 
      array (
        'company' => 'Three',
        'price' => 20,
      ),
    ),
  ),
  224 => 
  array (
    'id' => 224,
    'name' => 'Paul',
    'country' => 'France',
    'age' => 25,
    'companies' => 
    array (
      0 => 
      array (
        'company' => 'One',
        'price' => 25,
      ),
      1 => 
      array (
        'company' => 'Two',
        'price' => 40,
      ),
    ),
  ),
)

我尝试过这段代码,但没有得到好的结果:

$new_array = array();
foreach ($my_array as $item) {      
    $new_array[$item['id']][] = $item;
    $new_array[$item['id']]['companies'][] = $item;
    
}

然后我尝试了这个:

$new_array = array();
foreach ($my_array as $item) {      
    $new_array[$item['id']]['temp'] = $item;
    $new_array[$item['id']]['companies'][] = $item;         
}

工作正常,但我得到了我不想要的 temp 键。例如,使用此代码,我需要使用 $new_array [$key]['temp']['id']

访问 id 项目

我可以用另一个循环删除 temp 键:

$final_array = array();
foreach ($new_array $key=>$item) {      
    $final_array [$key] = $item['temp'];
    $final_array [$key]['temp'] = $item['companies'];
}

使用此代码,我可以通过以下方式正确访问 id:$final_array[$key]['id']

另一个选项是以下代码:

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

但是如果我们有更多的 key (电话、电子邮件......),那就不太优雅了

有什么想法吗?

最佳答案

您最后提供的代码没有任何问题,但这里有一些关于如何使其更能容忍更多数组值(如您提到的,电话、电子邮件等)的想法。

这使用了方便的 PHP array_diff_key函数从“核心”记录中删除不需要的数组元素。然后,应用array_diff_key将这些相同的数组元素放入“公司”记录中。

// set up the array of keys you don't want in the "original" record
$exclude = ['company' => FALSE, 'price' => FALSE];
// Your original loop
foreach($array as $v) {
    // not strictly necessary, but helps readability
    $id = $v['id'];
    // Assign the "core" record all the values you want, excluding those defined above
    // in this case, will remove "company" and "price"
    $record = array_diff_key( $v, $exclude );

    // only set this if not already set, otherwise wipes out previous companies
    if ( ! isset($result[$id] ) ) {
        $result[$id] = $record;
        $result[$id]['companies'] = [];
    }

    // strip off the OTHER values from the array you don't want stored with the company
    // this will automatically pick up the field NOT excluded above
    // in this case, will remove all BUT "company" and "price"
    $company = array_diff_key( $v, $record );
    $result[$id]['companies'][] = $company;
}

关于php - 按列对二维数组进行分组,保留多个其他列值并将 2 列值插入子数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41984640/

相关文章:

java - 数组如何在 C++ 或 Java 中为 "copied"

C++:二维数组,如何找到列中差异最大的行?

Javascript:如何在多维数组上实现迭代器?

php - 有没有办法在 MySQL 结果集中生成 "fake"行?

javascript - 使用下拉选择菜单突出显示一行表格单元格

php - 未知修饰符 'g' PHP 正则表达式错误

c++ - 以正确的方式将 3D 数组作为参数传递给 C++14 中的函数

php - 无法对我的用户数据库进行排序。

python - 如何在 Python 中对列表运行算法并将结果存储在列表中?

javascript - 如何展平嵌套引用中的数据?