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
  • 506
  • 2
  • 6
  • 18

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)
Muhammad Hassaan
  • 7,296
  • 6
  • 30
  • 50