简体   繁体   中英

How to parse array from MySQL column in PHP

I have column extra_fields in table.

In column i have array, and I need to parse it

 [{"id":"1","value":"fdsds"},{"id":"2","value":"\/images\/powered_by.png"},{"id":"3","value":"fdsfdsfdsfds"},{"id":"4","value":"<p>fdsfdsfdsfds<\/p>"}]

Try to do it with this code,

$extrafields = array();
foreach($this->item->extra_fields as $item)
{   
    $extrafields[$item->id] = $item->value;
}

but return empty string or...I don't know what

it is unclear as to what you want to achieve but here you go:

$test = json_decode('[{"id":"1","value":"fdsds"},{"id":"2","value":"\/images\/powered_by.png"},{"id":"3","value":"fdsfdsfdsfds"},{"id":"4","value":"<p>fdsfdsfdsfds<\/p>"}]');

$extrafields = array();
foreach ($test as $key => $item) {
    $extrafields[$item->id] = $item->value;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM