PHP: How to access Associative Array and solve "Array to String conversion notice"? -


edit: found mistake comments decoding json data.

i total rookie in php , couldn't find suitable method access associative array.

i have json data:

[{"id":"1"},{"id":"2"},{"id":"3"},{"id":"4"},{"id":"5"},{"id":"6"},{"id":"7"},{"id":"8"}] 

i need fire mysqli query in php code requires 1,2,3... above data.

implementing various solutions on site gives me array string conversion error. please help.

you can use array_column amd implode as

$json = '[{"id":"1"},{"id":"2"},{"id":"3"},{"id":"4"},{"id":"5"},{"id":"6"},{"id":"7"},{"id":"8"}]'; $data = implode(',',array_column(json_decode($json,true),'id')); echo $data;//1,2,3,4,5,6,7,8 

explanation:

  1. json_decode($json,true) convert json string array
  2. array_column(json_decode($json,true),'id') returns values single column of array, identified column_key i.e id on here
  3. implode join array elements glue string i.e ,.

Comments

Popular posts from this blog

How has firefox/gecko HTML+CSS rendering changed in version 38? -

javascript - Complex json ng-repeat -

jquery - Cloning of rows and columns from the old table into the new with colSpan and rowSpan -