数组php中的列表和计数元素

时间:2013-05-22 23:10:10

标签: php arrays

我有以下数组,我试图用php循环它,生成一个“类型”列表并计算多少。 Type是关键,但会有一些独特的值,例如Call,To-do,Meeting,Proposal等。我的目标是提供以下内容:

Call 2
To-do 1
Meeting 3
Proposal 4

以上不是将从以下数组输出的值,但我希望您了解我要完成的任务。请帮忙!

Array (
    [0] => Array (
        [0] => Call
        [Type] => Call
        [1] => fxxxx@xxxxentllc.com
        [EmailAddress] => xxxxr@xxxxentllc.com
        [2] => 3xxxx00
        [Phone] => 31xxxx00
        [3] => 31xxxx871
        [MobilePhone] => 31xxxx871
        [4] => 102795
        [CustomerID] => 102795
        [5] => Nortxxxxal
        [Company] => Noxxxxal
        [6] => Frank
        [FirstName] => Frank
        [7] => Syxxxxer
        [LastName] => Sxxxxter
        [8] => 3
        [Priority] => 3
        [9] => invite to Haxxxxales for lunch
        [Details] => invite to Hafxxxxales for lunch
        [10] => 4503
        [ActivityID] => 4503
        [11] => 05/23/13
        [DueDate] => 05/23/13
    )
    [1] => Array (
        [0] => To-do
        [Type] => To-do
        [1] => fsxxxxer@summxxxxntllc.com
        [EmailAddress] => fsxxxxer@summixxxxtllc.com
        [2] => 315xxxx000
        [Phone] => 3154xxxx0
        [3] => 315xxxx1
        [MobilePhone] => 315xxxx1
        [4] => 102795
        [CustomerID] => 102795
        [5] => Norxxxxl
        [Company] => Norxxxxcal
        [6] => Frxxxxk
        [FirstName] => Fxxxxk
        [7] => Sxxxxr
        [LastName] => Syxxxxer
        [8] => 3
        [Priority] => 3
        [9] => find out who contact is for xxxxdical center
        [Details] => find out who contact is foxxxxcal center
        [10] => 4504
        [ActivityID] => 4504
        [11] => 05/23/13
        [DueDate] => 05/23/13
    )
)

6 个答案:

答案 0 :(得分:3)

这应该这样做:

$type_counts = array_count_values(array_map(function($x) {return $x['Type'];}, $array));

array_map将返回一个包含所有Type元素的数组,然后array_count_values将计算每个元素的数量并将其作为关联数组返回。

答案 1 :(得分:1)

类似于:

foreach ($array as $key => $value) {
 if (isset($result[$key])) {
  $result[$key]++;
 } else {
 $result[$key] = 1;
 }
}

如果它是一个多维数组,只需在那里添加另一个,但这应该给你一个想法

答案 2 :(得分:0)

$types = array();
foreach($array as $item) {
    isset(${$item['Type']}) ? ${$item['Type']}++ : ${$item['Type']} = 1;
    if(!in_array($item['Type'], $types) {
        $types[] = $item['Type'];
}
foreach($types as $type) {
    echo "$type ${$type}\n";
}

答案 3 :(得分:0)

我认为你可以在数组中循环并计算每次出现次数

$Call = 0;
$To_do = 0;
$Meeting = 0;
$Proposal = 0;
foreach($array as $data)
{
    if($data['Type'] == 'Call')
    {
        $Call++;
    } else if($data['Type'] == 'To-do')
    {
        $To_do++;
    } else if($data['Type'] == 'Meeting')
    {
        $Meeting++;
    } else if($data['Type'] == 'Proposal')
    {
        $Proposal++;
    }
}

通过这种方式,您将在每个变量$Call $To_do $Meeting $Proposal中存储其相对计数

答案 4 :(得分:0)

你可以使用(array_count_values($ array)); http://php.net/manual/en/function.array-count-values.php 这给出了数组中所有键的计数。

如果只想要特定键,则使用foreach循环

Petros提到的是最好的方法。

答案 5 :(得分:0)

我会按照以下方式做点什么:

$your_array = /* Your Array */
$types_count = array();
foreach ($your_array as $type_array){
    $types_count[$type_array['Type']]++;
}