[英]PHP Extract and count first elements of each element in multidimensional array
I have this array in php, 我在php中有这个数组,
$mainArray = array(
array("apple","two", "grren"),
array("samsung","five", "red"),
array("microsoft","one","gray"),
array("apple","nine", "blue"),
array("samsung","ten", "white"),
array("nokia","seven", "yellow")
);
I can easily loop through it and extract all the first entries of each array like this: 我可以轻松地遍历它并提取每个数组的所有第一个条目,如下所示:
foreach($mainArray as $w => $n) {
$whatever = $mainArray[$w][0];
}
I'm trying to count how many entries are the same in the first element of each array, and have a result of something like: 我正在尝试计算每个数组的第一个元素中有多少条目相同,并且结果如下:
apple (2)
samsung (2)
microsoft (1)
nokia (1)
I'm just not sure what is the correct way to do this. 我只是不确定这样做的正确方法是什么。
Thank you in advance. 先感谢您。
print_r(
array_count_values(
array_map('array_shift', $mainArray)
)
);
Array
(
[apple] => 2
[samsung] => 2
[microsoft] => 1
[nokia] => 1
)
So even I am a big fan of foreach
, why did I not use it here? 所以,即使我是foreach
忠实粉丝,为什么我不在这里使用它?
First of all, to count values in an array, in PHP we have array_count_values
. 首先,要计算数组中的值,在PHP中我们有array_count_values
。 It does the job for us. 它为我们工作。
So the only problem left was to get all the first items into an array to count them with array_count_values
. 因此,剩下的唯一问题是将所有第一项都放入数组中,以使用array_count_values
计算它们。 That is a typical mapping job, and I like mapping, too, next to foreach
so I tried if array_map
together with array_shift
worked - and it did. 这是一个典型的映射工作,我也喜欢映射,在foreach
旁边,所以我尝试了如果array_map
与array_shift
一起工作 - 它确实如此。
However you might want to look for a function called array_column
. 但是,您可能希望查找名为array_column
的函数。 It's not yet available with PHP itself, but as PHP code in another answer : PHP本身尚不可用,但在另一个答案中作为PHP代码 :
$counts = array_count_values(array_column($mainArray, 0));
$count = array();
foreach($mainArray as $array) {
$first = $array[0];
if(!isset($count[$first])) $count[$first] = 0;
$count[$first]++;
}
print_r($count);
Collect every first element of the deep arrays by pushing them into a new array ( $result
in my example) and then call array_count_values()
on that array. 通过将深数组的每个第一个元素推入一个新数组(在我的示例中$result
),然后在该数组上调用array_count_values()
来收集它们的每个第一个元素。 Like so: 像这样:
$mainArray = array(
array("apple","two", "grren"),
array("samsung","five", "red"),
array("microsoft","one","gray"),
array("apple","nine", "blue"),
array("samsung","ten", "white"),
array("nokia","seven", "yellow")
);
$result = array();
foreach( $mainArray as $k => $v )
{
// let's continue if $v is not an array or is empty
if( !is_array( $v ) || empty( $v ) ) continue;
$result[] = $v[ 0 ];
}
var_dump( array_count_values( $result ) );
You can loop through the $mainArray
to build a full array/list of values and then use array_count_values()
on that: 你可以遍历$mainArray
来构建一个完整的数组/值列表,然后使用array_count_values()
:
$firstElements = array();
foreach ($mainArray as $arr) {
$firstElements[] = $arr[0];
}
$counts = array_count_values($firstElements);
Another option would be to loop through $mainArray
and insert the value as an index for an array (if it doesn't already exist) and then increment it each time (this will do the same thing as array_count_values()
in the end): 另一个选择是遍历$mainArray
并将值作为数组的索引插入(如果它尚不存在),然后每次递增它(这将与最后的array_count_values()
):
$counts = array();
foreach ($mainArray as $arr) {
if (!isset($counts[$arr[0]])) $counts[$arr[0]] = 0;
$counts[$arr[0]]++;
}
You can do it just like this: 你可以这样做:
foreach($mainArray as $n) {
$count[$n[0]] = isset($count[$n[0]]) ? $count[$n[0]]++ : 1;
}
var_dump($count); //should give you something like
/*
array(4) {
["apple"]=>
int(2)
["samsung"]=>
int(2)
["microsoft"]=>
int(1)
["nokia"]=>
int(1)
}
*/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.