I'm really stuck here. Im having an array looking like this below. And now I would like to count postStatus where postStatus = 0, for all of the arrays.

我真的被困在这里了。我有一个如下所示的阵列。现在我想计算postStatus,其中postStatus = 0,适用于所有数组。

So in this case there would be 2. But how do I do this?

所以在这种情况下会有2.但我该怎么做?

Array
(
[1] => Array
    (
        [postId] => 1
        [postHeader] => Post-besked #1
        [postContent] => Post content #1 
        [postDate] => 2011-12-27 17:33:11
        [postStatus] => 0
    )

[2] => Array
    (
        [postId] => 2
        [postHeader] => Post-besked #2 
        [postContent] => POst content #2
        [postDate] => 2011-12-27 17:33:36
        [postStatus] => 0
    )
)

3 个解决方案

#1


5

Just loop the outer array, check if there is a postStatus, increment a value to keep that count and you're done...

只需循环外部数组,检查是否有postStatus,增加一个值以保持计数,你就完成了......

$postStatus = 0;
foreach($myarray as $myarraycontent){
    if(isset($myarraycontent['postStatus']) && $myarraycontent['postStatus'] == 0){
        $postStatus++;
    }
}
echo $postStatus;

EDIT:

编辑:

I forgot to mention that isset() can be used but a better pratice is to use array_key_exists because if $myarraycontent['postStatus'] is NULL, it will return false. Thats the way isset() works...

我忘了提到可以使用isset()但是更好的实践是使用array_key_exists,因为如果$ myarraycontent ['postStatus']为NULL,它将返回false。这就是isset()的工作原理......

更多相关文章

  1. PHP用空格分割文本为数组的方法
  2. 如何将PHP数组的关联数组转移到javascript?
  3. PHP - 将字符串转换为键值数组
  4. 如何在不更改其键值的情况下删除数组的第一个元素?
  5. 以完整二叉树,数组格式获取所有节点
  6. php 写一个算法,查寻数组里第一个大于某数的值。
  7. 插入PHP数组作为MYSQL列
  8. php把从数据库读取出来的数据存放到数组里
  9. 在key/value的基础上将数组分组到另一个数组中

随机推荐

  1. 动画:面试必刷之找出数组中重复的数字
  2. 入土系列 | 前端入门实战项目(六)
  3. pip 最新版 发布(Python包安装和管理工具)
  4. Python处理文件的几个常用小知识
  5. 用 VS Code 写 Python,这8个扩展装上后无
  6. 超级好用的RabbitMQ 消息 100% 投递的解
  7. 入土系列 | 前端入门实战项目(四)
  8. 吊打面试官系列 | ES6 面试知识点精华篇(
  9. 动画:什么是堆?
  10. 动画:用动画给面试官解释 KMP 算法