为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

PHP
慕妹3242003 2019-10-17 14:48:39
我从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)知道我该怎么做吗?
查看完整描述

3 回答

?
拉丁的传说

TA贡献1789条经验 获得超8个赞

很简单,首先将您的对象转换为json对象,这会将您的对象字符串转换为JSON代表。


获取该结果并使用额外的参数true解码,它将转换为关联数组


$array = json_decode(json_encode($oObject),true);


查看完整回答
反对 回复 2019-10-17
?
慕的地8271018

TA贡献1796条经验 获得超4个赞

尝试这个:


$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;

    }

}


查看完整回答
反对 回复 2019-10-17
  • 3 回答
  • 0 关注
  • 539 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信