0

我有以下数组,并试图用 php 循环遍历它,生成“类型”列表并计算有多少。类型是关键,但会有唯一的值,如 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
    )
)
4

6 回答 6

3

这应该这样做:

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

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

于 2013-05-22T23:18:08.423 回答
1

类似于:

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

如果它是一个多维数组,只需在其中为每个数组放一个,但这应该会给你一个想法

于 2013-05-22T23:18:57.280 回答
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";
}
于 2013-05-22T23:20:05.240 回答
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其相对计数

于 2013-05-22T23:24:25.947 回答
0

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

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

Petros 提到的那个是最好的方法。

于 2013-05-22T23:24:56.447 回答
0

我会按照以下方式做一些事情:

$your_array = /* Your Array */
$types_count = array();
foreach ($your_array as $type_array){
    $types_count[$type_array['Type']]++;
}
于 2013-05-22T23:27:16.563 回答