1

I have two arrays like below:

array 1:

array([0]=> 11, [1] => 2.5)

array 2:

Array ( 
    [0] => stdClass Object ( 
        [type] => 1 
        [creator_id] => 3 
        [creator_name] => E1 
        [per_tar] => 300 
        [pro_tar] => 200 
        [ac] => 300 
        [PA] => 17 
        [Q1] => 800 
    ) 
    [1] => stdClass Object ( 
        [type] => 1 
        [creator_id] => 4 
        [creator_name] => E2 
        [per_tar] => 100 
        [pro_tar] => 170 
        [ac] => 0 
        [PA] => 7 
        [Q1] => 270 
    ) 
) 

I want is to merge the first array with the second array with the following logic:

Array 1 key [0] is merged inside array 2 key [0] object, and so on in the same fashion for all keys.

So my expected output is:

Array ( 
    [0] => stdClass Object ( 
        [type] => 1 
        [creator_id] => 3 
        [creator_name] => E1 
        [per_tar] => 300 
        [pro_tar] => 200 
        [ac] => 300 
        [PA] => 17 
        [Q1] => 800
        [new] => 11 
    ) 
    [1] => stdClass Object ( 
        [type] => 1 
        [creator_id] => 4 
        [creator_name] => E2 
        [per_tar] => 100 
        [pro_tar] => 170 
        [ac] => 0 
        [PA] => 7 
        [Q1] => 270
        [new] => 2.5 
    ) 
) 

I am trying the array_merge() function, but it's incorrectly merging both arrays into a four-element array.

4
  • 2
    Possible duplicate of array merge php with same index Commented Feb 24, 2017 at 8:40
  • i am getting this error array_merge(): Argument #1 is not an array Commented Feb 24, 2017 at 8:49
  • can you show any example Commented Feb 24, 2017 at 8:49
  • array-object has the method toArray(). If you have ArrayObjects nested inside the ArrayObject I am afraid this will not work, as toArray() does not recurse. You would need to solve this with a custom method. Commented Feb 24, 2017 at 9:02

3 Answers 3

2
$res = [];
foreach($array1 as $key => $val){
    // before merging convert object to array
    $arr = is_object($array2[$key]) ? (array)$array2[$key] : $array2[$key];  
    $res[$key] = array_merge($array1[$key], $arr);
}

print_r($res);
Sign up to request clarification or add additional context in comments.

Comments

2

You should iterate $array1 items and insert value of every item into relevant index of $array2.

foreach ($array1 as $key=>$value){
    $array2[$key]->new = $value;
}

See result in demo

Also if you want to remain array2, copy value of it in new variable like $newArray.

$newArray = $array2;
foreach ($array1 as $key=>$value){
    $newArray[$key]->new = $value;
}

2 Comments

i have an doubt. if my array 2 have 1 key means i am getting Creating default object from empty value error how to solve this
@Nisanth You should check if target index exist in $array2 then set new property in it. See 3v4l.org/GPRPV
-1

Best bet would be to convert the stdClass object to an array then just use array_merge.

convert the stdClass object to an array like so:

$array = json_decode(json_encode($object), true);

1 Comment

What's wrong with $array = (array) $object;

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.