count() not returning correct number of array elements

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.

Answers


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.


It's counting the contents of $_SESSION['licensed_users'], which is 1 - your $licensed_users array. You could instead try:

count($_SESSION['licensed_users'], COUNT_RECURSIVE);

This will return 7. You could approach this knowing your structure and use:

count($_SESSION['licensed_users'], COUNT_RECURSIVE) -1;

Which returns the expected 6.

To simplify a bit more, consider this:

$_SESSION['licensed_users'] = array_unique(array_merge($lu, $lu2));

Then count($_SESSION['licensed_users']; should return 6.


<?
$my_array = array(0 => 1, 1 => 2, 2 => 3, 3 => 4, 4 => 5);

$your_array = array(0 => 1, 1 => 2);

$the_array = array_unique(array_merge($my_array, $your_array)); 

$_SESSION['test'] = $the_array;

print_r($the_array);

echo(count($_SESSION['test']));
?>

yields:

Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 ) 5

<?
$my_array = array(0 => 1, 1 => 1, 2 => 1, 3 => 1, 4 => 1);

$your_array = array(0 => 1, 1 => 1);

$the_array = array_unique(array_merge($my_array, $your_array)); 

$_SESSION['test'] = $the_array;

print_r($the_array);

echo(count($_SESSION['test']));
?>

yields:

Array ( [0] => 1 ) 1

my guess would be your stdClass Object([id] => xxxxx) are all the same...

EDIT

another test:

<?
$my_array = array(0 => array('id' => 11), 1 => array('id' => 22), 
                  2 => array('id' => 22), 3 => array('id' => 33), 4 => array('id' => 445));

$your_array = array(0 => array('id' => 11), 1 => array('id' => 7908));

$the_array = array_unique(array_merge($my_array, $your_array), SORT_REGULAR); 

$_SESSION['test'] = $the_array;

print_r($the_array);

echo(count($_SESSION['test']));
?>

yields:

Array ( [0] => Array ( [id] => 11 ) [1] => Array ( [id] => 22 ) [3] => Array ( [id] => 33 ) [4] => Array ( [id] => 445 ) [6] => Array ( [id] => 7908 ) ) 5

I think this is what you're looking for.


Need Your Help

copying portions of string, which are stored in a char array, to another array

c arrays string copy char

I wanted to try and copy parts of strings which are already stored in one array of strings to another empty array. (I think called array of pointers to char arrays )