如何从JSON输出获取PHP数组

时间:2018-03-28 16:22:18

标签: php arrays

我有一个API请求,我需要它放入MySQL DB

 {
  "channel": {
    "id": 456221,
    "name": "CURRENT VALUES",
    "description": "Sensor Current ACS712 Values",
    "latitude": "0.0",
    "longitude": "0.0",
    "field1": "Current",
    "created_at": "2018-03-22T13:13:01Z",
    "updated_at": "2018-03-28T12:41:13Z",
    "last_entry_id": 37
  },
  "feeds": [
    {
      "created_at": "2018-03-28T12:41:13Z",
      "field1": "4.98534"
    }
  ]
}

虽然我使用foreach循环来检索值“feeds fields1”

$url = "https://api-url";
$string = file_get_contents($url);
$arr = json_decode($string, true);

foreach($arr ["feeds"] as $item){
    $entry_id = $item['entry_id'];
    $field1 = $item['field1'];

    mysql_query("INSERT INTO temperature (id_entry, field1) VALUES('$entry_id', '$field1')") or die (mysql_error());
}

我的目标是将“channel id”和“feed field1”放入一个数组中,然后在foreach循环中使用它们。

2 个答案:

答案 0 :(得分:0)

如果我将json解码为array,那么我可以看到以下输出。因此,如果您想访问id inside channelfield1 inside feeds,请尝试这样做。

<?php
$json_object = '{
  "channel": {
    "id": 456221,
    "name": "CURRENT VALUES",
    "description": "Sensor Current ACS712 Values",
    "latitude": "0.0",
    "longitude": "0.0",
    "field1": "Current",
    "created_at": "2018-03-22T13:13:01Z",
    "updated_at": "2018-03-28T12:41:13Z",
    "last_entry_id": 37
  },
  "feeds": [
    {
      "created_at": "2018-03-28T12:41:13Z",
      "field1": "4.98534"
    }
  ]
}';

$array = json_decode($json_object,1);
print '<pre>';
print_r($array);
print '</pre>';
?>

阵列输出:

Array
(
    [channel] => Array
        (
            [id] => 456221
            [name] => CURRENT VALUES
            [description] => Sensor Current ACS712 Values
            [latitude] => 0.0
            [longitude] => 0.0
            [field1] => Current
            [created_at] => 2018-03-22T13:13:01Z
            [updated_at] => 2018-03-28T12:41:13Z
            [last_entry_id] => 37
        )

    [feeds] => Array
        (
            [0] => Array
                (
                    [created_at] => 2018-03-28T12:41:13Z
                    [field1] => 4.98534
                )
        )
)

所以现在你可以像这样访问它,

<?php
echo "Channel Id is = ".$array['channel']['id'];
echo "\n";
echo "Feeds Field1 is = ".$array['feeds'][0]['field1'];
?>

阵列访问输出

Channel Id is = 456221
Feeds Field1 is = 4.98534  

答案 1 :(得分:0)

<?php
$json = '{
  "channel": {
    "id": 456221,
    "name": "CURRENT VALUES",
    "description": "Sensor Current ACS712 Values",
    "latitude": "0.0",
    "longitude": "0.0",
    "field1": "Current",
    "created_at": "2018-03-22T13:13:01Z",
    "updated_at": "2018-03-28T12:41:13Z",
    "last_entry_id": 37
  },
  "feeds": [
    {
      "created_at": "2018-03-28T12:41:13Z",
      "field1": "4.98534"
    }
  ]
}';

$array = json_decode($json, true);

$channel_id = $array['channel']['id']; // or $array['channel']['last_entry_id']
foreach ($array['feeds'] as $feed) {
    echo $channel_id . ':' . $feed['field1'];
    // update database (if $channel_id is your entry_id)
}

输出:

456221:4.98534