PHP将具有相同键的数组合并到一个自定义数组

时间:2017-10-10 10:32:14

标签: php arrays

我在数组中有多个来自mysql的结果

数组1

Array
(
  [type] => Food
  [storage] => S5213
  [quantity1] => 1000
  [in1] => 10/09/2017
  [quantity2] => 1000
  [in2] => 10/09/2017
)

数组2

Array
(
  [type] => Food
  [storage] => S4512
  [quantity1] => 990
  [in1] => 13/09/2017

)

阵列3

Array
(
  [type] => Drink
  [storage] => K4221
  [quantity1] => 12000
  [in1] => 12/09/2017
)

所以,如果来自不同数组的“type”之类的键相同并将其放入自定义数组中,我想合并。同时,对于不同的库存数量和日期,它可能有quantity1,quantity2,quantity3和in1,in2,in3。想把它合并到“架子”。

预期结果

Array
(
[Food] => Array
    (
        [0] => Array
            (
                [storage] => S5213
                [rack] => Array
                    (
                        [0] => Array
                            (
                                [quantity] => 1000
                                [in] => 10/09/2017
                            )

                        [1] => Array
                            (
                                [quantity] => 1000
                                [in] => 10/09/2017
                            )

                    )

            )

        [1] => Array
            (
                [storage] => S4512
                [rack] => Array
                    (
                        [0] => Array
                            (
                                [quantity] => 990
                                [in] => 13/09/2017
                            )

                    )

            )

    )

[Drink] => Array
    (
        [0] => Array
            (
                [storage] => K4221
                [rack] => Array
                    (
                        [0] => Array
                            (
                                [quantity] => 12000
                                [in] => 12/09/2017
                            )

                    )

            )

    )

)

有可能吗?尝试使用array_merge_recursive,但不输出预期结果。

1 个答案:

答案 0 :(得分:1)

如果这是您的SQL查询返回的内容,那么看起来您的数据库未规范化。更具体地说,您不应在单个表中包含列quantity1quantity2,...等。规则是,如果您有多个字段,则创建一个单独的表。在这种情况下,该表应该只有一个外键,可能是一个序列号(1,2,...),最后是一个quantity列。多个数量将在该附加表中表示为多行。可以添加in字段,因为它似乎遵循相同的规则。

无论如何,对于给定的情况,你可以使用这个PHP函数:

function addArray(&$main, $arr) {
    if (!is_array($main)) $main = []; // first time
    $main[$arr["type"]][] = [
        "storage" => $arr["storage"],
        "rack" => array_reduce(array_keys($arr), function ($acc, $key) use ($arr) {
            if (strpos($key, "quantity") === 0) {
                $acc[] = [
                    "quantity" => $arr[$key],
                    "in" => $arr[str_replace("quantity", "in", $key)]
                ];
            }
            return $acc;
        }, [])
    ];
}

按如下方式使用:

$arr = [
  "type" => "Food",
  "storage" => "S5213",
  "quantity1" => 1000,
  "in1" => "10/09/2017",
  "quantity2" => 1000,
  "in2" => "10/09/2017"
];
addArray($main, $arr);

$arr = [
  "type" => "Food",
  "storage" => "S4512",
  "quantity1" => 990,
  "in1" => "13/09/2017"
];
addArray($main, $arr);

$arr = [
  "type" => "Drink",
  "storage" => "K4221",
  "quantity1" => 12000,
  "in1" => "12/09/2017"
];
addArray($main, $arr);

...等$main将具有所需的结构。