0

array1:

array(1) {
  [0]=>
  array(2) {
    ["1234"]=>
    array(1) {
      ["fileName"]=>
      string(10) "monkey.jpg"
    }
    ["3456"]=>
    array(1) {
      ["fileName"]=>
      string(9) "horse.jpg"

    }
  }
}

array2:

array(2) {
  ["number"]=>
  string(2) "62"
  ["company"]=>
  string(7) "animals"
}

I want to merge the two arrays together:

$result = array_merge_recursive($array1,$array2);

This is the result:

array(3) {
  [0]=>
  array(2) {
    ["1234"]=>
        array(1) {
          ["fileName"]=>
          string(10) "monkey.jpg"
        }
     ["3456"]=>
        array(1) {
          ["fileName"]=>
          string(9) "horse.jpg"   
     }
  }
  ["number"]=>
  string(2) "62"
  ["company"]=>
  string(7) "animals"
}

But the result I would actually need is this:

array(1) {
  [0]=>
  array(4) {
     ["1234"]=>
          array(1) {
         ["fileName"]=>
          string(10) "monkey.jpg"
         }
      ["3456"]=>
         array(1) {
            ["fileName"]=>
           string(9) "horse.jpg"   
         }
      ["number"]=>
      string(2) "62"
      ["company"]=>
      string(7) "animals"
  }
}

How could I achieve this?


I test array_push like RaymondNijland suggested:

array_push($array1[0],$array2);

But now the $array1 looks like this:

array(1) {
  [0]=>
  array(3) {
    ["1234"]=>
     array(1) {
     ["fileName"]=>
      string(10) "monkey.jpg"
       }
    ["3456"]=>
       array(1) {
        ["fileName"]=>
        string(9) "horse.jpg"   
      }
    }
    [0]=>
    array(2) {
        ["number"]=>
         string(2) "62"
         ["company"]=>
         string(7) "animals"
    }
  }
}

Still not the result I am looking for

7
  • "How could I achieve this?" Looks to me just as simple as $array1[0][] = $array2 or array_push($array1[0], $array2) Commented May 16, 2018 at 18:14
  • @RaymondNijland I just tested your suggestion and my result is int(3). Nothing more. What did I do wrong? Commented May 16, 2018 at 18:18
  • You should provide actual PHP code not some output what looks to be var_dump() Commented May 16, 2018 at 18:20
  • @RaymondNijland My code is $result = array_push($array1[0], $array2) Commented May 16, 2018 at 18:21
  • var_dump($array1) after the array_push($array1[0], $array2) then you will see that $array1 is changed.. Commented May 16, 2018 at 18:25

2 Answers 2

1

Since you want to add to the existing array, iterate through the new array and add each element of it.

Here's the function and example usage:

function array_merge_recursive_custom($array1, $array2){
    $result[] = $array1;

    foreach($array2 as $key=>$value){
        $result[0][$key] = $value;
    }
    return $result;
}

$array1 = ['1234' => ['fileName' => 'monkey.jpg'], '3456' => ['fileName' => 'horse.jpg']];
$array2 = ['number' => '62', 'company' => 'animals'];

$result = array_merge_recursive_custom($array1, $array2);

var_dump($result);

Output:

array(1) {
  [0]=>
  array(4) {
    [1234]=>
    array(1) {
      ["fileName"]=>
      string(10) "monkey.jpg"
    }
    [3456]=>
    array(1) {
      ["fileName"]=>
      string(9) "horse.jpg"
    }
    ["number"]=>
    string(2) "62"
    ["company"]=>
    string(7) "animals"
  }
}
Sign up to request clarification or add additional context in comments.

Comments

1
$array3 = $array1;
foreach ($array2 as $key => $value) {
  $array3[0][$key] = $value;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.