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:
json_decode($json,true)
convertjson
string arrayarray_column(json_decode($json,true),'id')
returns values single column of array, identified column_key i.eid
on hereimplode
join
array elements glue string i.e,
.
Comments
Post a Comment