1

i have some problem how merger this array. could help me ?

First array :

Array
    (
        [22] => WP_Post Object
            (
                [ID] => 22
                [post_author] => 1

            )

        [23] => WP_Post Object
            (
                [ID] => 23
                [post_author] => 1

            )

    )

Second array :

Array
    (
        [0] => stdClass Object
            (
                [img_thumb] => small_duck.jpg
                [img_full] => duck.jpg
            )

        [1] => stdClass Object
            (
                [img_thumb] => small_fish.jpg
                [img_full] => fish.jpg
            )

    )

Should OutPut :

   Array
        (
            [22] => WP_Post Object
                (
                    [ID] => 22
                    [post_author] => 1
                    [img_thumb] => small_duck.jpg
                    [img_full] => duck.jpg

                )

            [23] => WP_Post Object
                (
                    [ID] => 23
                    [post_author] => 1
                    [img_thumb] => small_fish.jpg
                    [img_full] => fish.jpg

                )

        )

The array key follow first array,

1

1 Answer 1

1

This works...

$first = array(
    22 => array(
        'ID' => 22,
        'post_author' => 1
    ),
    23 => array(
        'ID' => 23,
        'post_author' => 1
    )
);
$second  = array(
    array(
        'img_thumb' => 'small_duck.jpg',
        'img_full' => 'duck.jpg'
    ),
    array(
        'img_thumb' => 'small_fish.jpg',
        'img_full' => 'fish.jpg'
    )
);

echo '<pre>';
var_dump($first);
var_dump($second);

$i=0;
$should = array();
foreach ($first as $key => $arr) {
    if(isset($second[$i]))
        $arr = array_merge($arr,$second[$i]);

    $should[$key] = $arr;
    $i++;
}

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

1 Comment

SOLVED, Thank for @Barry

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.