-1

I have an API request and I need it to put in 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"
    }
  ]
}

while I use foreach loop to retrieve values "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());
}

My aim is to get the "channel id" and "feeds field1" into an array and then use them in a foreach loop.

Nug Dol
  • 3
  • 2

2 Answers2

0

If I decode you json to array then I can see below output. So if you want to access id inside channel or field1 inside feeds then try like this way.

<?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>';
?>

OUTPUT OF ARRAY:

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
                )
        )
)

So Now you can access it like this,

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

OUTPUT OF ARRAY ACCESS

Channel Id is = 456221
Feeds Field1 is = 4.98534  
A l w a y s S u n n y
  • 36,497
  • 8
  • 60
  • 103
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)
}

OUTPUT:

456221:4.98534

Michael Eugene Yuen
  • 2,470
  • 2
  • 17
  • 19
  • The key is that, you can access variable declared earlier inside the foreach loop as long as it's within the same scope. http://php.net/manual/en/language.variables.scope.php – Michael Eugene Yuen Mar 28 '18 at 16:53