将stdClass对象转换为PHP中的数组

我从postmeta获取post_id为:


$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); 我有这样的数组:


Array

(

    [0] => stdClass Object

        (

            [post_id] => 140

        )


    [1] => stdClass Object

        (

            [post_id] => 141

        )


    [2] => stdClass Object

        (

            [post_id] => 142

        )


)

而且我不知道如何遍历它,如何获得这样的数组


Array

(

    [0]  => 140



    [1] => 141



    [2] => 142


)

知道我该怎么做吗?


慕妹3242003
浏览 540回答 3
3回答

拉丁的传说

很简单,首先将您的对象转换为json对象,这会将您的对象字符串转换为JSON代表。获取该结果并使用额外的参数true解码,它将转换为关联数组$array = json_decode(json_encode($oObject),true);

慕的地8271018

尝试这个:$new_array = objectToArray($yourObject);function objectToArray($d) {    if (is_object($d)) {        // Gets the properties of the given object        // with get_object_vars function        $d = get_object_vars($d);    }    if (is_array($d)) {        /*        * Return array converted to object        * Using __FUNCTION__ (Magic constant)        * for recursive call        */        return array_map(__FUNCTION__, $d);    } else {        // Return array        return $d;    }}
打开App,查看更多内容
随时随地看视频慕课网APP