如何在php中附加此数组

如何在php中附加此数组

问题描述:

I need to append this array in php script .Kindly help to do it perfectly .I tried multiple solution but none resulted to correct solution.

Array(
    [Donn] => 0
    [Lamo] => 0
    [Otis] => 0
    [Stev] => 0
    [Matt] => 0
    [Samm] => 0
    [Andr] => 0
    [Jerr] => 0
    [Simm] => 0
    [Steph] => 0
    [Fredd] => 0
    [Willi] => 0

)

to the following array

Array(
[Don] => Array
    (
        [Ab] => 1
        [Ang] => 1
        [Ant] => 2
        [Bo] => 1
        [Ch] => 1
        [Chri] => 2
        [Chri] => 4
        [Deau] => 1
        [Der] => 1
        [Sylveste] => 1
    )

[Lam] => Array
    (
        [Ab] => 2
        [Ch] => 22
        [Dona] => 1
        [Irw] => 1
        [Kou] => 1
        [Llo] => 1
        [Ro] => 1
        [Shumy] => 1
    )

[Oti] => Array
    (
        [Ab] => 1
        [Arla] => 1
        [Kour] => 1
        [Osh] => 1
        [Roy ] => 1
        [Tim] => 1
        [War] => 1
        //add the given array here
    ))

So that the result is

Array([Don] => Array
    (
        [Ab] => 1
        [Ang] => 1
        [Ant] => 2
        [Bo] => 1
        [Ch] => 1
        [Chri] => 2
        [Chri] => 4
        [Deau] => 1
        [Der] => 1
        [Sylveste] => 1
    )

[Lam] => Array
    (
        [Ab] => 2
        [Ch] => 22
        [Dona] => 1
        [Irw] => 1
        [Kou] => 1
        [Llo] => 1
        [Ro] => 1
        [Shumy] => 1
    )

[Oti] => Array
    (
        [Ab] => 1
        [Arla] => 1
        [Kour] => 1
        [Osh] => 1
        [Roy ] => 1
        [Tim] => 1
        [War] => 1
        [Donn] => 0
[Lamo] => 0
[Otis] => 0
[Stev] => 0
[Matt] => 0
[Samm] => 0
[Andr] => 0
[Jerr] => 0
[Simm] => 0
[Steph] => 0
[Fredd] => 0
[Willi] => 0
        //added here
    ))

Please use least loops as much as possible.So as to get time feasible solution.Use php array function if possible.

Use foreach to add into the oti index:

<?php

$firstArr = Array(
"Don" => Array
    (
        "Ab" => 1,
        "Ang" => 1,
        "Ant" => 2,
        "Bo" => 1,
        "Ch" => 1,
        "Chri" => 2,
        "Chri" => 4,
        "Deau" => 1,
        "Der" => 1,
        "Sylveste" => 1,
    ),

"Lam" => Array
    (
        "Ab" => 2,
        "Ch" => 22,
        "Dona" => 1,
        "Irw" => 1,
        "Kou" => 1,
        "Llo" => 1,
        "Ro" => 1,
        "Shumy" => 1,
    ),

"Oti" => Array
    (
        "Ab" => 1,
        "Arla" => 1,
        "Kour" => 1,
        "Osh" => 1,
        "Roy " => 1,
        "Tim" => 1,
        "War" => 1,
        //add the given array here
    ));

$secondArr = Array(
    "Donn" => 0,
    "Lamo" => 0,
    "Otis" => 0,
    "Stev" => 0,
    "Matt" => 0,
    "Samm" => 0,
    "Andr" => 0,
    "Jerr" => 0,
    "Simm" => 0,
    "Steph" => 0,
    "Fredd" => 0,
    "Willi" => 0,

);

foreach ($secondArr as $key => $value) {
    $firstArr["Oti"][$key] = $value;
}

print_r($firstArr);

Demo


You can also use array_merge as @Mark Baker said in comments:

$firstArr["Oti"] = array_merge($firstArr["Oti"], $secondArr);

This should solve it, you use the array_merge function to merge the 2 arrays :)

$secondArray['Oti'] = array_merge($secondArray['Oti'], $firstArray);