Convert wpdb sql array into php array

2019-09-03 08:17发布

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”;}}

2条回答
ら.Afraid
2楼-- · 2019-09-03 08:52

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);
查看更多
爷、活的狠高调
3楼-- · 2019-09-03 09:10

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 "

查看更多
登录 后发表回答