简体   繁体   English

找出php中数组出现的次数

[英]Find out number of array occurrences in php

this answer maybe on google but i couldnt find it because i dont know the exact keywords for this. 这个答案也许在谷歌,但我找不到它,因为我不知道确切的关键字。 The situation is as follows. 情况如下。 I have an array called $value that contains 2 types of arrays: 我有一个名为$ value的数组,其中包含2种类型的数组:

[12] => Array ( 
        [id] => phone 
        [class] => phone 
        [config] => Array ( 
                            [disabled] => 1 
                            [value] => 1
                )
) 

[46] => Array ( 
        [id] => email 
        [class] => email 
        [config] => Array ( 
                            [display_type] => disabled 
                            [value] => 1
                    )
)

As you may see, they are almost the same except for the fact that one contains an array called "disabled" and the other "display_type". 如您所见,它们几乎相同,除了一个包含一个名为“ disabled”的数组和另一个“ display_type”的数组。 What im trying to do is to count how many of each type i have. 我想做的是计算每种类型的数量。 I tried with this: 我尝试了这个:

foreach ( $value as $col ) {
    if ( $col->config == 'disabled' ) {
        $total_default++;
    } elseif ( $col->config == 'display_type' ) {
        $total_custom++;
    }
}

but didnt work. 但没有工作。 I know it must be very simple but i just cant figure it out. 我知道它必须非常简单,但我无法弄清楚。

Thank you. 谢谢。

$col is an array, not object. $col是一个数组,而不是对象。

foreach ($value as $col) {
  if (array_key_exists('disabled', $col['config'])) {
      $total_default++;
  } else if (array_key_exists('display_type', $col['config'])) {
      $total_custom++;
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM