[英]PHP - Count unique in an array of objects
I have a PHP array which contains objects like this我有一个包含这样的对象的 PHP 数组
Array
(
[0] => stdClass Object
(
[label] => Test 1
[session] => 2
)
[1] => stdClass Object
(
[label] => Test 2
[session] => 2
)
[2] => stdClass Object
(
[label] => Test 3
[session] => 42
)
[3] => stdClass Object
(
[label] => Test 4
[session] => 9
)
)
I am trying to count the number of unique sessions within this array.我正在尝试计算此数组中唯一会话的数量。 I can do it when the whole thing is an array, but I am struggling to work it out when the array contains objects.当整个事物是一个数组时,我可以做到这一点,但是当数组包含对象时,我正在努力解决它。
Do I need to convert the objects into arrays or is there a way of doing it with the data in its current format?我是否需要将对象转换为数组,或者是否可以使用当前格式的数据进行转换?
https://www.php.net/manual/en/function.array-column.php : https://www.php.net/manual/en/function.array-column.php :
Version Description 7.0.0 Added the ability for the input parameter to be an array of objects.
Use array_column to generate new keys using the session column values.使用 array_column 使用会话列值生成新键。 This effectively removes duplicate keys.这有效地删除了重复的键。
$array = [
(object)['label' => 'Test 1', 'session' => 2],
(object)['label' => 'Test 2', 'session' => 2],
(object)['label' => 'Test 3', 'session' => 42],
(object)['label' => 'Test 4', 'session' => 9],
];
echo sizeof(array_column($array, null, 'session'));
Output:输出:
3
Or in a loop:或者在一个循环中:
foreach ($array as $obj) {
$result[$obj->session] = null;
}
echo sizeof($result);
Both techniques avoid the extra function call of array_unique
and leverage the fact that arrays cannot store duplicate keys.这两种技术都避免了array_unique
的额外函数调用,并利用了数组不能存储重复键的事实。
I have tried your code and here created sample data我已经尝试过你的代码并在这里创建了示例数据
$comments= array();
$comment = new stdClass;
$comment->label = 'Test 1';
$comment->session = '2';
array_push($comments, $comment);
$comment = new stdClass;
$comment->label = 'Test 2';
$comment->session = '2';
array_push($comments, $comment);
$comment = new stdClass;
$comment->label = 'Test 3';
$comment->session = '42';
array_push($comments, $comment);
$comment = new stdClass;
$comment->label = 'Test 4';
$comment->session = '9';
array_push($comments, $comment);
Here is code I tried to get the unique value.这是我试图获得唯一值的代码。 this way you can get any field unique value这样你就可以得到任何字段的唯一值
$uniques = array();
foreach ($comments as $obj) {
$uniques[$obj->session] = $obj;
}
echo "<pre>";
print_r($uniques);
echo "</pre>";
You could also use array_map to only keep the value of session
in the array, then use array_unique to remove the duplicate entries and finally count the unique items.您也可以使用array_map仅将session
的值保留在数组中,然后使用array_unique删除重复条目,最后计算唯一项。
If for example your array variable is called $array
:例如,如果您的数组变量被称为$array
:
$result = array_map(function($x){
return $x->session;
}, $array);
echo count(array_unique($result));
That will result in:这将导致:
3 3
The object within an array can be accessed with $array[0]
where the 0
stands for the object.数组中的对象可以通过$array[0]
访问,其中0
代表对象。 To access the objects property you can do $object->session
.要访问 objects 属性,您可以执行$object->session
。
To go throught every objects session property you can do:要遍历每个对象会话属性,您可以执行以下操作:
foreach ($array as $object) {
echo $object->session . "<br/>";
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.