Convert wpdb sql array into php array

走远了吗. 提交于 2020-01-06 08:22:12

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!