count()没有返回正确数量的数组元素

时间:2011-12-01 16:00:04

标签: php arrays

我正在合并两个数组(从数据库查询返回的记录)。然后我需要计算组合数组中的元素。

以下是两个原始数组的print_r结果:

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)
)

合并后的数组如下:

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

合并的结果(在这种情况下没有任何重复,但可能有,因此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)
)

将数组分配给会话变量,以便在另一个页面上使用:

    $_SESSION['licensed_users'] = $licensed_users;

我现在想知道通过会话变量在合并数组中有多少元素。

count($_SESSION['licensed_users'])

我希望这会返回6.相反,它会返回1.任何想法为什么?

已编辑为@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";

此代码返回以下内容:

$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)
)

回显线返回1.

如果我按照你建议的另一种方式尝试:

$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";

返回的数组具有相同的内容,但回显线返回-1。

3 个答案:

答案 0 :(得分:1)

它计算$ _SESSION ['licensed_users']的内容,即1 - 你的$ licensed_users数组。你可以尝试:

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

这将返回7.你可以通过了解你的结构和使用来解决这个问题:

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

返回预期的6。

为了简化一点,请考虑以下几点:

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

然后count($_SESSION['licensed_users'];应该返回6.

答案 1 :(得分:0)

问题出在array_unique()电话中。来自文档:

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

这些对象在被比较之前被转换为字符串。您可能必须根据其他属性手动修剪它们。

答案 2 :(得分:0)

<?
$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']));
?>

的产率:

数组([0] =&gt; 1 [1] =&gt; 2 [2] =&gt; 3 [3] =&gt; 4 [4] =&gt; 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']));
?>

的产率:

数组([0] =&gt; 1)1

我的猜测是你的stdClass对象([id] =&gt; xxxxx)都是一样的......

修改

另一项测试:

<?
$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']));
?>

的产率:

数组([0] =&gt;数组([id] =&gt; 11)[1] =&gt;数组([id] =&gt; 22)[3] =&gt;数组([id] =&gt; ; 33)[4] =&gt;数组([id] =&gt; 445)[6] =&gt;数组([id] =&gt; 7908))5

我认为这就是你要找的东西。