0

How to ech

[{"date":"2020-05-01","stats":[{"metrics":{"blocks":118,"bounce_drops":80,"bounces":148,"clicks":2642,"deferred":716,"delivered":73681,"invalid_emails":2,"opens":8072,"processed":67638,"requests":74028,"spam_report_drops":0,"spam_reports":2,"unique_clicks":1510,"unique_opens":4274,"unsubscribe_drops":0,"unsubscribes":0}}]}] 

but i am not sure how i can echo certain value, i have created a code, but it doesn't work.

I just want to echo specific terms, like requests.

<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

$response = "[{"date":"2020-05-01","stats":[{"metrics":{"blocks":118,"bounce_drops":80,"bounces":148,"clicks":2642,"deferred":716,"delivered":73681,"invalid_emails":2,"opens":8072,"processed":67638,"requests":74028,"spam_report_drops":0,"spam_reports":2,"unique_clicks":1510,"unique_opens":4274,"unsubscribe_drops":0,"unsubscribes":0}}]}]";

$json = json_decode($response,true);
foreach ($json['stats']['metrics']['requests'] as $person) {
  echo $person;
}
$characters = json_decode($response, true); // decode the JSON feed
foreach ( $characters as $stats => $array ) {   
    $characters = $array['requests'];
  echo $array['requests'];
  echo $characters;
}
  echo $array['requests'];
  echo $characters;
?>
Harsh
  • 152
  • 1
  • 1
  • 9
  • $response is an Array, you should access `metrics` it like this `foreach ($json[0]['stats'][0]['metrics'] as $person) {` – ROOT May 21 '20 at 12:51
  • @ROOT then i have to have `echo $person[requests];` ? – Harsh May 21 '20 at 13:12
  • if you want to access it directly, just use `$json[0]['stats'][0]['metrics']['requests']` without looping through response. – ROOT May 21 '20 at 13:15
  • @ROOT that throws out error `Invalid argument supplied for foreach() ` – Harsh May 21 '20 at 13:18
  • 1
    you should not use loop if you want to access it directly: check this: https://3v4l.org/N07rr – ROOT May 21 '20 at 13:21
  • That works, please make it an answer :D i will mark it. – Harsh May 21 '20 at 13:46
  • I can't because your question is closed :) since it was posted, you can support me with upvates on other answer :P – ROOT May 21 '20 at 13:50

0 Answers0