如何从PHP中的多维数组中获取相同值的计数?

时间:2015-11-06 11:26:37

标签: php arrays codeigniter multidimensional-array

这是我的数据库表

enter image description here

我正在爆炸这些值

<?php 
    $i =0;  
    foreach($appointment as $app_book){ 
       $arr[$i] = explode(",",$app_book['type_of_ultra_sound']); 
       $i++;
    } 
    echo "<pre>";
    print_r($arr);
    echo "</pre>";
?>

输出是

enter image description here

我试过这个

$occurences = array_count_values($items);
print_r($occurences);

我也试过这个但没有帮助 enter link description here

我需要的是整个数组的计数类似的值 多普勒(臂动脉)= 2 ......

4 个答案:

答案 0 :(得分:1)

$values = [];
foreach($array as $s) {
    foreach($s as $ss) {
        if (!isset($values[$ss])) {
            $values[$ss]= 0;
        }
        $values[$ss]++;
    }
}

答案 1 :(得分:1)

您可以将值逐个存储在数组中,而不是创建多维数组。

<?php
$i =0;
$arr = array();
foreach($appointment as $app_book) {
    $exp = explode(",",$app_book['type_of_ultra_sound']);
    foreach($exp as $v)
    {
        $arr[$i] = $v;
        $i++;
    }
}
echo '<pre>';
print_r($arr);
echo '</pre>';

$occurences = array_count_values($arr);
print_r($occurences);

foreach($occurences as $keys => $values){
 echo $keys ." ". $values ."<br>";
}

?>

答案 2 :(得分:0)

在您的情况下,一个简单的嵌套循环就足够了:

// $arr is your array
$counts = array();
foreach($arr as $suba) {
    foreach($suba as $doppler) {
        (isset($counts[$doppler])) || $counts[$doppler] = 0;
         $counts[$doppler] += 1;
    }
}

示例运行:

[25] boris> $arr;
// array(
//   0 => array(
//     0 => 'doppler a',
//     1 => 'doppler b'
//   ),
//   1 => array(
//     0 => 'doppler c',
//     1 => 'doppler b'
//   ),
//   2 => array(
//     0 => 'doppler a',
//     1 => 'doppler b',
//     2 => 'doppler c',
//     3 => 'doppler d'
//   )
// )
[26] boris> $counts = array();
// array(
//   
// )
[27] boris> foreach($arr as $suba) {
[27]     *>     foreach($suba as $doppler) {
[27]     *>         (isset($counts[$doppler])) || $counts[$doppler] = 0;
[27]     *>         $counts[$doppler] += 1;
[27]     *>     }
[27]     *> }
[28] boris> $counts;
// array(
//   'doppler a' => 2,
//   'doppler b' => 3,
//   'doppler c' => 2,
//   'doppler d' => 1
// )
[29] boris> 

答案 3 :(得分:0)

除非你需要单独的&#34;组&#34;你可以展平阵列,例如与

$usounds = [];
foreach($appointment as $app_book) { 
    $usounds = array_merge(
        $usounds,
        explode(',', $app_book['type_of_ultra_sound'])
    );
}

然后您可以使用array_count_values

自包含的例子:

<?php
$appointment = [
    [ 'type_of_ultra_sound'=>'Doppler (Arm Ateries),Doppler (Both Limbs)' ],
    [ 'type_of_ultra_sound'=>'Doppler (Arm Veins),Doppler (Liver)' ],
    [ 'type_of_ultra_sound'=>'Doppler (Arm Ateries),Doppler (Arm Veins),Doppler (Both Legs),Doppler (Both Limbs)' ]
];
$usounds = [];
foreach($appointment as $app_book) { 
    $usounds = array_merge(
        $usounds,
        explode(',', $app_book['type_of_ultra_sound'])
    );
}

var_export( array_count_values($usounds) );

打印

array (
  'Doppler (Arm Ateries)' => 2,
  'Doppler (Both Limbs)' => 2,
  'Doppler (Arm Veins)' => 2,
  'Doppler (Liver)' => 1,
  'Doppler (Both Legs)' => 1,
)