【问题标题】:How to merge array on behalf of a key value如何代表键值合并数组
【发布时间】:2020-09-30 06:48:49
【问题描述】:

我有一个数组

{
Array
(
    [0] => Array
        (
            [filterType] => checkbox
            [sfid] => a1d1I000000jrmwQAA
            [name] => Publication Year
            [specValues] => Array
                (
                    [count] => 3
                    [value] => 1953
                )

        )

    [1] => Array
        (
            [filterType] => checkbox
            [sfid] => a1d1I000000jrmwQAA
            [name] => Publication Year
            [specValues] => Array
                (
                    [count] => 1
                    [value] => 1954
                )

        )

)



}

但如果 sfid 相同,我想合并数组,即我想要结果

{
Array
(
    [0] => Array
        (
            [filterType] => checkbox
            [sfid] => a1d1I000000jrmwQAA
            [name] => Publication Year
            [specValues] => Array(
                                  [0]     =>array(
                                                    [count] => 3
                                                    [value] => 1953
                                                )
                                  [1]     =>array(
                                                    [count] => 1
                                                    [value] => 1954
                                                )

                                    )
         )
         )


}

【问题讨论】:

  • 到目前为止你完成了吗?你能提供你的代码吗?
  • 我仍在努力获得我想要的结果,但我失败了
  • 嗨,欢迎来到 StackOverflow。请阅读stackoverflow.com/help/minimal-reproducible-example,向我们展示您的代码,并向我们展示失败的原因。

标签: php arrays merge


【解决方案1】:

您只需要迭代您的数组并将该行添加到输出中(如果它还没有)(您可以使用 sfid 作为键)并将 specValues 添加到正确的行。

<?php

$input = [
  [ 
    'filterType' => 'checkbox',
    'sfid' => 'a1d1I000000jrmwQAA',
    'name' => 'Publication Year',
    'specValues' => [
      'count' => 3,
      'value' => 1953,
    ],
  ],
  [ 
    'filterType' => 'checkbox',
    'sfid' => 'a1d1I000000jrmwQAA',
    'name' => 'Publication Year',
    'specValues' => [
      'count' => 1,
      'value' => 1954,
    ],
  ],
];

$data = [];
foreach ($input as $row) {
  if (!isset($data[$row['sfid']])) {
    $data[$row['sfid']] = [
      'filterType' => $row['filterType'],
      'sfid' => $row['sfid'],
      'name' => $row['name'],
      'specValues' => [],
    ];
  } 
  
  $data[$row['sfid']]['specValues'][] = $row['specValues'];
}

var_dump(array_values($data));

输出:

array(1) {
  [0]=>
  array(4) {
    ["filterType"]=>
    string(8) "checkbox"
    ["sfid"]=>
    string(18) "a1d1I000000jrmwQAA"
    ["name"]=>
    string(16) "Publication Year"
    ["specValues"]=>
    array(2) {
      [0]=>
      array(2) {
        ["count"]=>
        int(3)
        ["value"]=>
        int(1953)
      }
      [1]=>
      array(2) {
        ["count"]=>
        int(1)
        ["value"]=>
        int(1954)
      }
    }
  }
}

我假设如果 sfid 相同,那么 filterType 和 name 也相同。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-06-12
    • 2017-02-25
    • 2013-07-01
    • 2018-11-27
    • 2020-09-08
    • 2018-06-29
    • 2017-02-03
    • 1970-01-01
    相关资源
    最近更新 更多