0

This is my old array.

$oldarray = Array
    (
        [0] => http://test.to/getac/l4p0y6ziqt9h
        [mock] => stdClass Object
            (
                [0] => http://test.to/getae/vidzichawal1
                [1] => http://test.to/getae/vidzi6
                [4] => http://test.to/getae/1x5fbr9t64xn
                [2] => http://test.to/getae/vidzi7
            )

    )

which i want to merge with this new array:

$newarray =  Array
    (
        [mock] => Array
            (
                [0] => http://test.to/getae/vidzichawal2
            )

    )

I am merging array by array_merge_recursive($oldarray, $newarray);

And the Result is this:

Array
(
    [0] => http://test.to/getac/l4p0y6ziqt9h
    [mock] => Array
        (
            [0] => http://test.to/getae/vidzi5
            [1] => http://test.to/getae/vidzi6
            [4] => http://test.to/getae/1x5fbr9t64xn
            [2] => http://test.to/getae/vidzi7
            [0] => http://test.to/getae/vidzichawal1
        )
);

All things is working good but there is one problem you can see in result there double 0 key when i am using this link in loop only 1 link retrive of 0 i want to set this keys automatically like 0 1 2 3 4 5 6 and go on after merging.

I hope you understand what i want thanks

Sufyan
  • 486
  • 2
  • 6
  • 16

1 Answers1

0

Use array_merge()

<?php
$array1 = array("color" => "red", 2, 4);
$array2 = array("a", "b", "color" => "green", "shape" => "trapezoid", 4);
$result = array_merge($array1, $array2);
print_r($result);
?>

The above example will output:

Array
(
    [color] => green
    [0] => 2
    [1] => 4
    [2] => a
    [3] => b
    [shape] => trapezoid
    [4] => 4
)

REF.

Note You can't have duplicate keys!

UPDATE

Example using array_merge_recursive()

<?php
$oldarray = array('http://test.to/getac/l4p0y6ziqt9h', 'mock' => array('http://test.to/getae/vidzichawal1', 'http://test.to/getae/vidzi6', 'http://test.to/getae/1x5fbr9t64xn', 'http://test.to/getae/vidzi7'));
$newarray = array('mock' => array('http://test.to/getae/vidzichawal2'));

$result = array_merge_recursive($oldarray, $newarray);

var_dump($result);
?>

OUTPUT

array (size=2)
  0 => string 'http://test.to/getac/l4p0y6ziqt9h' (length=33)
  'mock' => 
    array (size=5)
      0 => string 'http://test.to/getae/vidzichawal1' (length=33)
      1 => string 'http://test.to/getae/vidzi6' (length=27)
      2 => string 'http://test.to/getae/1x5fbr9t64xn' (length=33)
      3 => string 'http://test.to/getae/vidzi7' (length=27)
      4 => string 'http://test.to/getae/vidzichawal2' (length=32)
Hassaan
  • 6,770
  • 5
  • 29
  • 47
  • @Sufyan what issue do you face while using `array_merge()` – Hassaan Jul 29 '15 at 10:08
  • by using array_merge all link in oldarray mock were escape and newarray mock will put in this means its not merging right its replace new with old – Sufyan Jul 29 '15 at 10:15