如何在json数据laravel 5.1中查询Builder

时间:2016-08-04 03:33:24

标签: php json api laravel guzzle

如何在json数据中查询构建器?我正在使用guzzle,我想知道如何查询数量为非NULL的搜索数据?

json数据结果:

{
  "amount": [
    {
      "member_id": "SSD",
      "sum4": null,
      "sum5": null,
      "sum6": null
    },
    {
      "member_id": "OGG",
      "sum4": 122,
      "sum5": 242,
      "sum6": 123
    },
    {
      "member_id": "PUCAN",
      "sum4": 123,
      "sum5": 443,
      "sum6": 222
    },
    {
      "member_id": "SS3TG",
      "sum4": null,
      "sum5": null,
      "sum6": null
    }
  ]
}

所有我想知道如何得到sum4,sum6,sum6在JSON结果中不是空的,你们可以帮帮我..

1 个答案:

答案 0 :(得分:0)

// Pass JSON data with true as parameters to json_decode function
// It returns value as associative array
$data = json_decode($jsonData, true);

// Loop through amount
foreach($data['amount'] as $sum)
{
    // Check sum is not null
    if ($sum['sum4'] != null) {
        echo $sum['sum4'];
    // Check sum is not null
    } elseif ($sum['sum5'] != null) {
        echo $sum['sum5'];
    // Check sum is not null
    } elseif ($sum['sum6'] != null) {
        echo $sum['sum6'];
    }
}