如何确定是否设置了 JSON 数组,如果是,则输出它


How to determine if JSON array is set and output it if yes

所以,我有以下var_dump() JSON:

array(36) {
    [0]=> string(4) "null" 
    [1]=> string(56) "{"article":"Bread","==":"String"}" 
    [2]=> string(68) "{"article":"Beef","amount":3,"==":"String"}" 
    [3]=> string(4) "null" 
    [4]=> string(52) "{"article":"Water","==":"String"}" 
    [5]=> string(4) "null" 
    [6]=> string(4) "null" 
    [7]=> string(4) "null"
}

现在我想检查是否设置了 0,1,2...,然后输出"文章"和设置的金额(如果有)。

最优化的方法是什么?丑陋的 7 如果不是,或者?

您可以使用

foreach结构循环访问数组,json_decode将JSON转换为PHP对象,然后打印特定属性。

foreach ($your_array as $key => $value) {
    if ($value != 'null') {
        $data = json_decode($value);
        echo $data->article;
    }
}