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

  • SEARCH
  • Home
  • 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 6898883
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T07:21:07+00:00 2026-05-27T07:21:07+00:00

I have two arrays (records returned from a database query) that I’m merging. I

  • 0

I have two arrays (records returned from a database query) that I’m merging. I then need to get a count of the elements in the combined array.

Here are the print_r results of the two original arrays:

Array(
    [0] => stdClass Object([id] => 25590)
    [1] => stdClass Object([id] => 40657)
    [2] => stdClass Object([id] => 60685)
    [3] => stdClass Object([id] => 61900)
    [4] => stdClass Object([id] => 65224)
)

Array(
    [0] => stdClass Object([id] => 88406)
)

Merged array created like this:

$licensed_users = array_unique(array_merge($lu, $lu2));

And the results of the merge (in this case there weren’t any duplicates, but there could be, hence the array_unique)

Array(
    [0] => stdClass Object([id] => 25590)
    [1] => stdClass Object([id] => 40657)
    [2] => stdClass Object([id] => 60685)
    [3] => stdClass Object([id] => 61900)
    [4] => stdClass Object([id] => 65224)
    [5] => stdClass Object([id] => 88406)
)

The array is assigned to a session variable, to be used on another page:

    $_SESSION['licensed_users'] = $licensed_users;

I now want to know how many elements are in the merged array via the session variable.

count($_SESSION['licensed_users'])

I would expect this to return 6. Instead, it returns 1. Any idea why?

EDITED TO ADD CODE FOR @SURREALDREAMS

$_SESSION['licensed_users'] = array_unique(array_merge($lu, $lu2));
print_r($lu);
print_r($lu2);
print_r($_SESSION['licensed_users']);
echo "there are ". count($_SESSION['licensed_users']) . " licensed users";

This code returns the following:

$lu Array(
    [0] => stdClass Object([id] => 25590)
    [1] => stdClass Object([id] => 40657)
    [2] => stdClass Object([id] => 60685)
    [3] => stdClass Object([id] => 61900)
    [4] => stdClass Object([id] => 65224)
)

$lu2 Array(
    [0] => stdClass Object([id] => 88406)
)

$_SESSION['licensed_users'] Array(
    [0] => stdClass Object([id] => 25590)
)

The echo line returns 1.

If I try it the other way you suggested:

$licensed_users = array_unique(array_merge($lu, $lu2));
$_SESSION['licensed_users'] = $licensed_users;
echo "there are ". count($_SESSION['licensed_users'], COUNT_RECURSIVE) -1 . " licensed users";

the arrays returned have the same contents, but the echo line returns -1.

  • 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-27T07:21:08+00:00Added an answer on May 27, 2026 at 7:21 am

    The issue is in the array_unique() call. From the documentation:

    Two elements are considered equal if and only if (string) $elem1 === (string) $elem2. In words: when the string representation is the same.
    

    Those objects are being converted to strings before being compared. You may have to manually prune them based on some other property.

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

Sidebar

Related Questions

I have two arrays. One contains id=>count and the other contains id=>name . I'm
I have two Numpy record arrays that have exactly the same fields. What is
I have a database (SQL) with the table Staff with two records in it.
I have a two dimensional array that I need to load data into. I
I have two PHP arrays like so: Array of X records containing the ID
I'm having trouble crafting a fairly simple query with Doctrine... I have two arrays
I have two arrays of animals (for example). $array = array( array( 'id' =>
I have two arrays of System.Data.DataRow objects which I want to compare. The rows
I have two arrays containing the same elements, but in different orders, and I
I have two arrays in PHP. The first array ($author_array) is comprised of user_ids

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.