stdClass的对象转换为PHP数组
问题描述:
我取POST_ID从postmeta为:
I fetch post_id from postmeta as:
$post_id = $wpdb->get_results("SELECT post_id FROM $wpdb->postmeta WHERE (meta_key = 'mfn-post-link1' AND meta_value = '". $from ."')");
当我尝试的print_r($ POST_ID);
我有数组是这样的:
when i try print_r($post_id);
I have array like this:
Array
(
[0] => stdClass Object
(
[post_id] => 140
)
[1] => stdClass Object
(
[post_id] => 141
)
[2] => stdClass Object
(
[post_id] => 142
)
)
和我不知道如何遍历它,我怎么能拿阵列像这样
and i dont know how to traverse it, and how could I get array like this
Array
(
[0] => 140
[1] => 141
[2] => 142
)
任何想法,我怎么能做到这一点.......在此先感谢
Any idea how can I do this.......Thanks in advance
答
最简单的方法是JSON-EN code你的对象,然后去code回数组:
The easiest way is to JSON-encode your object and then decode it back to an array:
$array = json_decode(json_encode($object), True);
或者,如果你preFER,您可以手动遍历对象,也:
Or if you prefer, you can traverse the object manually, too:
foreach ($object as $value)
$array[] = $value->post_id;