Foreach does not take $value

Asked

Viewed 28 times

-2

mine foreach is only returning to $key and the $value displays only Array. What could be wrong with my code?

$curl_handle=curl_init();
curl_setopt($curl_handle, CURLOPT_URL,'https://www.instagram.com/p/CLm2laRsp8I/?__a=1');
curl_setopt($curl_handle, CURLOPT_CUSTOMREQUEST, "GET");
curl_setopt($curl_handle, CURLOPT_CONNECTTIMEOUT, 2);
curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, 1);
$query = curl_exec($curl_handle);
$data = json_decode($query, true);
curl_close($curl_handle);

foreach ($data["graphql"]["shortcode_media"]["edge_media_to_parent_comment"]["edges"] as $array) {
foreach ($array as $key => $value) {
print "$key : $value<br>";
}
}

Return:

Warning: Array to string conversion
node : Array
Warning: Array to string conversion
node : Array
  • As you said yourself, your value is an array, you cannot print as if it were a string.

1 answer

-1


You are using print to display a array, should wear print_r($value,true). This function will return the value and will transform into string.

print "$key : ". print_r($value,true) . "<br>"
  • print "$key : ". print_r($value,true) . " <br>"

  • You can edit the reply to include the information in the comment. There are some typos in your reply, could take advantage to make these corrections.

  • Thank you! Problem solved.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.