简体   繁体   English

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

[英]PHP merge arrays with same key to one custom array

I have multiple result from mysql in array like this 我在这样的数组中从mysql获得多个结果

Array1 数组1

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

Array 2 阵列2

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

)

Array 3 阵列3

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

So, i would like to merge if the key such as "type" from different arrays are same and put it into custom array. 因此,如果不同的数组中的“类型”之类的键相同,我想将其合并到自定义数组中。 At the same time, it maybe has quantity1, quantity2, quantity3 and in1,in2,in3 for different stocks quantity and date. 同时,对于不同的库存数量和日期,它可能具有数量1,数量2,数量3和in1,in2,in3。 Would like to merge it into "rack". 想要将其合并为“机架”。

Expected result 预期结果

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
                            )

                    )

            )

    )

)

Is it possible? 可能吗? tried using array_merge_recursive, but not output expected result. 尝试使用array_merge_recursive,但未输出预期结果。

If this is what your SQL query returns, then it looks like your database is not normalised. 如果这是您的SQL查询返回的内容,则表明您的数据库未规范化。 More concretely, you should not have columns quantity1 , quantity2 , ...etc in a single table. 更具体地讲,您不应在单个表中包含quantity1quantity2等的列。 The rule is, if you have more than one of some field, create a separate table. 规则是,如果某个字段中有多个字段,请创建一个单独的表。 In this case that table should just have a foreign key, maybe a sequence number (1, 2, ...) and finally a single quantity column. 在这种情况下,该表应该只具有一个外键,可能是一个序列号(1、2,...),最后是一个quantity列。 Multiple quantities would be expressed as multiple rows in that additional table. 在该附加表中,多个数量将表示为多行。 The in field could be added to that, as it seems to follow the same rule. in字段可以添加到该字段,因为它遵循相同的规则。

Anyway, for the given situation, you could use this PHP function: 无论如何,对于给定的情况,您可以使用以下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;
        }, [])
    ];
}

Use it as follows: 如下使用它:

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

... etc. $main will have the desired structure. ...等。 $main将具有所需的结构。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM