Preview via JSONArray in PHP

I get this error:

A warning. Invalid argument provided by foreach () on [page] .php on line 49

This is an echo of the $ json variable: [{"d":"2011-03-26","q":1,"t":1060},{"d":"2011-03-26","q":2,"t":1060},{"d":"2011-03-26","q":1,"t":1060}]

And I am trying to iterate as follows:

 foreach($json as $obj) { // <--THIS IS LINE 49 // Stuff } 
+4
source share
4 answers

Just to guess:

Your $json variable is a string. You will need to convert it to an object using json_decode to iterate over the object:

 $json_obj = json_decode( $json ); foreach( $json_obj as $obj ) { //stuff } 
+6
source

you must decode json before you can repeat it.

The JSON string itself does not make sense for foreach.

+1
source

First try using json_decode() . It looks like your variable is encoded by json, which means it's really just a string and therefore not an enum foreach.

 foreach(json_decode($json) as $obj) { // stuff } 
+1
source

foreach(json_decode($json) as $obj) { // stuff }

It returns me a warning similar to this: Invalid argument provided by foreach (), although it works.

My code is here:

 function search_terms ( $json , $term ) { if ( $json != null ){ foreach ( $json as $item ) {// Recursive function $this->search_terms ( $item, $term ); } }else{ } } 
0
source

Source: https://habr.com/ru/post/1345365/


All Articles