使用php迭代json对象

时间:2017-05-01 18:53:26

标签: php sql json parsing codeigniter-3

我目前有一个json对象数组,并且只想获取状态字段。我需要验证是否status == 0。如果状态等于0,那么我希望它能做点什么。

我想遍历每个对象并获取状态值,以便输出取决于每个对象中指定的值。如何改进我的代码来迭代并正确获取每个对象的状态列?

 [  
       {  
          "user_id":"1",
          "notification_id":"7",
          "id":"7",
          "product_id":"4233",
          "message":"2",
          "type":"2",
          "created_at":"1493618633",
          "status":"0"
       },
       {  
          "user_id":"1",
          "notification_id":"8",
          "id":"8",
          "product_id":"4233",
          "message":"4",
          "type":"0",
          "created_at":"1493618663",
          "status":"0"
       },
       {  
          "user_id":"1",
          "notification_id":"9",
          "id":"9",
          "product_id":"4232",
          "message":"45",
          "type":"1",
          "created_at":"1493618784",
          "status":"0"
       }
    ]

模型

public function getNotifications($timestamp, $user_id){
        $sql =  "SELECT * FROM storelte_user_notifications LEFT JOIN storelte_notifications ON storelte_user_notifications.notification_id = storelte_notifications.id  WHERE created_at >= ? AND user_id = ? ORDER BY created_at";
        $response = $this->db->query($sql, array($timestamp, $user_id));
        return $response->result_array();
    }

我是如何尝试在json中获取数据的

$array = $this->notification->getNotifications($timestamp, $user_id);
        if ($array > 0) {
            if (empty(array_filter(array_column($array, 'status'))) == 0) { 
                echo 'unread'; 
            }
        }

0 个答案:

没有答案