使用不同的键合并PHP数组

时间:2016-02-26 14:00:49

标签: php arrays merge array-merge

我想合并两个数组。

第一个看起来像这样:

 array(28) {
  [0]=>
  array(17) {
    ["category_id"]=>
    string(2) "11"
    ["image"]=>
    string(27) "718426050751.jpg"
    ["parent_id"]=>
    string(1) "1"
    ["top"]=>
    string(1) "0"
    ["column"]=>
    string(1) "1"
    ["sort_order"]=>
    string(2) "21"
    ["status"]=>
    string(1) "1"
    ["date_added"]=>
    string(19) "2015-07-30 14:06:42"
    ["date_modified"]=>
    string(19) "2016-01-05 12:21:32"
    ["language_id"]=>
    string(1) "1"
    ["name"]=>
    string(7) "Faucets"
    ["description"]=>
    string(0) ""
    ["meta_description"]=>
    string(0) ""
    ["meta_keyword"]=>
    string(0) ""
    ["store_id"]=>
    string(1) "1"
    ["product_count"]=>
    string(1) "0"
    ["page_count"]=>
    string(1) "0"
  }

第二个看起来像这样:

array(17) {
  ["category_id"]=>
  string(2) "13"
  ["image"]=>
  string(28) "4005176268779.jpg"
  ["parent_id"]=>
  string(2) "11"
  ["top"]=>
  string(1) "0"
  ["column"]=>
  string(1) "1"
  ["sort_order"]=>
  string(1) "2"
  ["status"]=>
  string(1) "1"
  ["date_added"]=>
  string(19) "2015-07-30 14:06:43"
  ["date_modified"]=>
  string(19) "2016-01-07 14:10:53"
  ["language_id"]=>
  string(1) "1"
  ["name"]=>
  string(12) "Sink Faucets"
  ["description"]=>
  string(0) ""
  ["meta_description"]=>
  string(0) ""
  ["meta_keyword"]=>
  string(0) ""
  ["store_id"]=>
  string(1) "1"
  ["product_count"]=>
  string(1) "0"
  ["page_count"]=>
  string(1) "0"
}

正如你所看到的那样,第二个没有像第一个那样的密钥[0]。

当我使用时:

$children = array_merge((array)$children, (array)$additional);

我得到一个合并的数组,但附加数组没有得到一个对我来说最好是[1]或最后一个数组键+1的键。

是否可以这样做?

由于

2 个答案:

答案 0 :(得分:0)

$array = [
    "0" => ["key1"=>"val1", "key2"=>"val2"]
    ];

$merge =  ["key1"=>"val3", "key2"=>"val4"];

array_merge($array, [$merge]);

结果:

Array
(
    [0] => Array
        (
            [key1] => val1
            [key2] => val2
        )

    [1] => Array
        (
            [key1] => val3
            [key2] => val4
        )

)

http://sandbox.onlinephpfunctions.com/code/a2e7abb2d9580b5f487d56b99a782505633c840b

答案 1 :(得分:0)

detectChanges没有必要,你只需要将第二个数组推入第一个数组。 array_merge()会将$a[]=$b;的数据添加到$b,并在没有函数调用的情况下分配自动递增的密钥。

代码:

$a

输出:

$a=[
    0=>[
        "category_id"=>"11",
        "image"=>"718426050751.jpg",
        "parent_id"=>"1",
        "top"=>"0",
        "column"=>"1",
        "sort_order"=>"21",
        "status"=>"1",
        "date_added"=>"2015-07-30 14:06:42",
        "date_modified"=>"2016-01-05 12:21:32",
        "language_id"=>"1",
        "name"=>"Faucets",
        "description"=>"",
        "meta_description"=>"",
        "meta_keyword"=>"",
        "store_id"=>"1",
        "product_count"=>"0",
        "page_count"=>"0"
    ]
];
$b=[
    "category_id"=>"13",
    "image"=>"4005176268779.jpg",
    "parent_id"=>"11",
    "top"=>"0",
    "column"=>"1",
    "sort_order"=>"2",
    "status"=>"1",
    "date_added"=>"2015-07-30 14:06:43",
    "date_modified"=>"2016-01-07 14:10:53",
    "language_id"=>"1",
    "name"=>"Sink Faucets",
    "description"=>"",
    "meta_description"=>"",
    "meta_keyword"=>"",
    "store_id"=>"1",
    "product_count"=>"0",
    "page_count"=>"0"
];
$a[]=$b;  // push, not merge
var_export($a);