-1

I have two Array Like this, The array_replace_recursive() not working in this case, Since i edited the same for more clarification.

Array
(
    [0] => stdClass Object
        (
            [Author] => 1
            [totalComments] => 5
            [commentsPoints] => 900
            [commentDateDiffpoints] => 460
        )

    [1] => stdClass Object
        (
            [Author] => 2
            [totalComments] => 4
            [commentsPoints] => 720
            [commentDateDiffpoints] => 24
        )
    [2] => stdClass Object
        (
            [Author] => 3
            [totalComments] => 4
            [commentsPoints] => 720
            [commentDateDiffpoints] => 24
        )

    [3] => stdClass Object
        (
            [Author] => 18
            [totalComments] => 4
            [commentsPoints] => 720
            [commentDateDiffpoints] => 24
        )

)

AND

Array
(
    [0] => stdClass Object
        (
            [Author] => 1
            [totalLikesGiven] => 5
            [likesGivenOnTopicPoints] => 36
            [likesGivenOnReplyPoints] => 108
            [likesGivenOnBlogPoints] => 36
            [DateDiffTopicpoints] => 1
            [DateDiffReplypoints] => 3
            [DateDiffBlogpoints] => 1
        )

    [1] => stdClass Object
        (
            [Author] => 3
            [totalLikesGiven] => 1
            [likesGivenOnTopicPoints] => 0
            [likesGivenOnReplyPoints] => 0
            [likesGivenOnBlogPoints] => 36
            [DateDiffTopicpoints] => 0
            [DateDiffReplypoints] => 0
            [DateDiffBlogpoints] => 1
        )

)

I like a result LIKE this

Array
(
    [0] => stdClass Object
        (
            [Author] => 1
            [totalComments] => 5
            [commentsPoints] => 900
            [commentDateDiffpoints] => 460
            [totalLikesGiven] => 5
            [likesGivenOnTopicPoints] => 36
            [likesGivenOnReplyPoints] => 108
            [likesGivenOnBlogPoints] => 36
            [DateDiffTopicpoints] => 1
            [DateDiffReplypoints] => 3
            [DateDiffBlogpoints] => 1
        )
    [1] => stdClass Object
        (
            [Author] => 2
            [totalComments] => 4
            [commentsPoints] => 720
            [commentDateDiffpoints] => 24
        )

    [2] => stdClass Object
        (
            [Author] => 3
            [totalComments] => 4
            [commentsPoints] => 720
            [commentDateDiffpoints] => 24
            [totalLikesGiven] => 1
            [likesGivenOnTopicPoints] => 0
            [likesGivenOnReplyPoints] => 0
            [likesGivenOnBlogPoints] => 36
            [DateDiffTopicpoints] => 0
            [DateDiffReplypoints] => 0
            [DateDiffBlogpoints] => 1
        )

    [3] => stdClass Object
        (
            [Author] => 18
            [totalComments] => 4
            [commentsPoints] => 720
            [commentDateDiffpoints] => 24
        )

)

Please help how can I do the same in PHP.

1
  • A tip for objects in arrays. It helps immensely mostly the primary key in your database to use as a key for the array. So that you can more quickly find your objects Commented Jul 5, 2016 at 5:41

2 Answers 2

0

Just use array_replace_recursive:

$finalArr = array_replace_recursive($arr1,$arr2);

There is a similar thread. And more such questions in SO.

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks It works with just an update, and that is convert both the stdClass Objects to an array by using $arr1 = json_decode(json_encode($arr1), true); $arr2 = json_decode(json_encode($arr2), true); AND finally use $finalArr = array_replace_recursive($arr1,$arr2);
The array_replace_recursive() function not working when the first and second both array has unique values, it is not adding both records.
Then go for array_merge_recursive in that case
OK, now i got multiple records against the same [Author] how to group them in a single record within the same array, Thanks.
-2

Use array merge php function. PHP array merge function

2 Comments

link only answers are discouraged.
@Dagon, ok. two more minuses and you achieve your goal

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.