dvp dvp - 4 months ago 10
PHP Question

Merge Two Array in PHP got from a select query : UPDATE

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.

Answer

Just use array_replace_recursive:

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

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

Comments