提取每个逗号分隔的数组字的计数

时间:2015-08-16 18:22:58

标签: php arrays

我想从后面的数组中提取每个逗号分隔的单词的计数。尝试了多个函数(递归/非递归)但似乎没有任何工作。

array_count_values 函数只将所有键的组合计数设为3,而它应该为每个字提供输出3,因为所有3个子数组都是相同的。

实际数组

Array
(
   [0] => Bangalore, Chennai, Delhi, Gurgaon, Hyderabad, Kolkata, Mumbai / Navi Mumbai, Noida, Guntur
   [1] => Bangalore, Chennai, Delhi, Gurgaon, Hyderabad, Kolkata, Mumbai / Navi Mumbai, Noida, Guntur
   [2] => Bangalore, Chennai, Delhi, Gurgaon, Hyderabad, Kolkata, Mumbai / Navi Mumbai, Noida, Guntur
)

This Link似乎相当接近&提供以下输出

Array
(
   [Bangalore] => 1
   [Chennai] => 1
   [Delhi] => 1
   [Gurgaon] => 1
   [Hyderabad] => 1
   [Kolkata] => 1
   [Mumbai / Navi Mumbai] => 1
   [Noida] => 1
   [Guntur] => 1
)

所需输出

Array
(
   [Bangalore] => 3
   [Chennai] => 3
   [Delhi] => 3
   [Gurgaon] => 3
   [Hyderabad] => 3
   [Kolkata] => 3
   [Mumbai / Navi Mumbai] => 3
   [Noida] => 3
   [Guntur] => 3
)

我知道我非常接近所需的输出但无法修复它。

3 个答案:

答案 0 :(得分:1)

尝试使用array_merge(),然后array_count_values()

如果你的行是数组:

$arr[] = array("Bangalore", "Chennai", "Delhi", "Gurgaon", "Hyderabad", "Kolkata", "Mumbai / Navi Mumbai", "Noida", "Guntur");
$arr[] = array("Bangalore", "Chennai", "Delhi", "Gurgaon", "Hyderabad", "Kolkata", "Mumbai / Navi Mumbai", "Noida", "Guntur");
$arr[] = array("Bangalore", "Chennai", "Delhi", "Gurgaon", "Hyderabad", "Kolkata", "Mumbai / Navi Mumbai", "Noida", "Guntur");

$comp = array();
foreach($arr as $vals) {
    $comp   =   array_merge($vals,$comp);
}

print_r(array_count_values($comp));

如果您的行是字符串,则还需要explode()

<?php
$arr[] = "Bangalore,Chennai,Delhi,Gurgaon,Hyderabad,Kolkata,Mumbai / Navi Mumbai,Noida,Guntur";
$arr[] = "Bangalore,Chennai,Delhi,Gurgaon,Hyderabad,Kolkata,Mumbai / Navi Mumbai,Noida,Guntur";
$arr[] = "Bangalore,Chennai,Delhi,Gurgaon,Hyderabad,Kolkata,Mumbai / Navi Mumbai,Noida,Guntur";

$comp = array();
foreach($arr as $vals) {
        $new    =   explode(",",$vals);
        $comp   =   array_merge($new,$comp);
    }

print_r(array_count_values($comp));
?>

给你:

Array
(
    [Bangalore] => 3
    [Chennai] => 3
    [Delhi] => 3
    [Gurgaon] => 3
    [Hyderabad] => 3
    [Kolkata] => 3
    [Mumbai / Navi Mumbai] => 3
    [Noida] => 3
    [Guntur] => 3
)

答案 1 :(得分:0)

<?php

function soFnc($pcs,$arrResult) {
    foreach($pcs AS $val) {
        $val = trim($val);
        if(isset($arrResult[$val]))
            $arrResult[$val]++; 
        else
            $arrResult[$val]=1; 
    }   
    return $arrResult;
}

$arr = array();
$arr[] = 'Bangalore, Chennai, Delhi, Gurgaon, Hyderabad, Kolkata, Mumbai / Navi Mumbai, Noida, Guntur';
$arr[] = 'Bangalore, Chennai, Delhi, Gurgaon, Hyderabad, Kolkata, Mumbai / Navi Mumbai, Noida, Guntur';
$arr[] = 'Chennai, Delhi, Gurgaon, Hyderabad, Kolkata, Mumbai / Navi Mumbai, Noida, Guntur';

foreach($arr AS $item)
{

    if(!isset($arrResult))
        $arrResult=array();

    $pices = explode(",",$item);
    $arrResult = soFnc($pices,$arrResult);
}


echo "<pre>"; print_r($arrResult); echo "</pre>";

答案 2 :(得分:0)

忍不住提出一个单线; - )

$input = array(
    'foo, bar, baz',
    'foo, bar',
    'foo',
);

$output = array_count_values(call_user_func_array('array_merge', array_map(function($v) { return explode(', ', $v); }, $input)));

产生:

Array
(
    [foo] => 3
    [bar] => 2
    [baz] => 1
)