Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3754922
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T09:37:09+00:00 2026-05-19T09:37:09+00:00

I have 2 associative arrays: $arr1 & $arr2. I’d like to create $arr3, which

  • 0

I have 2 associative arrays: $arr1 & $arr2. I’d like to create $arr3, which would combine ‘name’ and ‘character’ if the dates match…if the dates don’t match, then just the character:

Here’s $arr1:

Array
(
[0] => stdClass Object
    (
        [date] => 2010/01/01
        [name] => Mario Lopez
    )

[1] => stdClass Object
    (
        [date] => 2010/01/02
        [name] => Lark Voorhies
    )

)

Here’s $arr2:

Array
(
[0] => Array
    (
        [date] => 2010/01/01
        [character] => AC Slater
    )

[1] => Array
    (
        [date] => 2010/01/02
        [character] => Lisa Turtle
    )
[2] => Array
    (
        [date] => 2010/01/03
        [character] => Kelly Kapowski
    )
)

Using array_intersect gives the following error: “Object of class stdClass could not be converted to string”.

Here’s what I’d like to get, if it’s possible (ie $arr3):

Array
(
[0] => stdClass Object
    (
        [date] => 2010/01/01
        [name] => Mario Lopez
        [character] => AC Slater
    )

[1] => stdClass Object
    (
        [date] => 2010/01/02
        [name] => Lark Voorhies
        [character] => Lisa Turtle
    )

[2] => stdClass Object
    (
        [date] => 2010/01/03
        [character] => Kelly Kapowski
    )

)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T09:37:10+00:00Added an answer on May 19, 2026 at 9:37 am

    This function was posted on php.net and I’ve been using it for quite a while. It should do what you are asking for

    function array_extend($a, $b) {
        foreach($b as $k=>$v) {
            if( is_array($v) ) {
                if( !isset($a[$k]) OR isset($v[0])) {
                    $a[$k] = $v;
                } else {
                    $a[$k] = array_extend($a[$k], $v);
                }
            } else {
                $a[$k] = $v;
            }
        }
        return $a;
    }
    

    Usage:

    $array = array_extend($orig_array,$new_array);
    

    Note that you will either have to convert your objects to arrays or modify the function to convert the object to an array on the fly ($a = (array) $a);

    Edit:

    Original source http://www.php.net/manual/en/function.array-merge.php#95294

    Note that I made a small modification to resolve an issue with the given function where it would not properly extend an array with numeric keys

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to have a unique sort function for several associative arrays. The
If I have two associative arrays, what would be the most efficient way of
I have two sequential (non-associative) arrays whose values I want to combine into a
I wanted to make an associative array in Javascript in which I would have
I need to merge associative arrays and group by the name. Say I have
I have an associative array in awk that gets populated like this: chr_count[$3]++ When
I have a hierarchically nested associative array. It looks like this: A = {
I have an indexed array which I've generated from an associative array with this
I have a bunch of php arrays that look like this (please don't ask
I have two associative arrays in PHP that are defined as following: $this_week[] =

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.