问题
With an sql query I'm grabbing the meta_value of a specific row from my wpdb. In sql it's an array, but it outputs in php as a string, like this:
a:1:{i:0;a:2:{i:0;s:10:”02/17/2014″;i:1;s:10:”Thom Stark”;}}
How can I convert this into a php array, so I can access the values from array 2's indices? My query looks like this:
$tabledata = $wpdb->get_row("SELECT meta_value FROM ".$prefix."frm_item_metas WHERE field_id='$fid' AND item_id='$eid'");
$data = $tabledata->meta_value;
echo $data;
outputs: a:1:{i:0;a:2:{i:0;s:10:”02/17/2014″;i:1;s:10:”Thom Stark”;}}
回答1:
You can use unserialize
$string = 'a:1:{i:0;a:2:{i:0;s:10:”02/17/2014″;i:1;s:10:”Thom Stark”;}}';
$array = unserialize($string);
回答2:
Its a serialized array so u need to use unserialize to convert it back to array as
$string = 'a:1:{i:0;a:2:{i:0;s:10:"02/17/2014";i:1;s:10:"Thom Stark";}}';
$array = unserialize(trim($string));
print_r($array);
NOTE : you have ”
it should be "
来源:https://stackoverflow.com/questions/22682602/convert-wpdb-sql-array-into-php-array