开发者社区 问答 正文

将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

) 知道我该怎么做吗? 问题来源于stack overflow

展开
收起
保持可爱mmm 2020-02-08 10:43:41 475 分享
分享
版权
举报
1 条回答
写回答
取消 提交回答
  • 最简单的方法是对对象进行JSON编码,然后将其解码回数组:

    $array = json_decode(json_encode($object), True); 或者,如果您愿意,也可以手动遍历对象:

    foreach ($object as $value) $array[] = $value->post_id;

    2020-02-08 10:44:01 举报
    赞同 评论

    评论

    全部评论 (0)

    登录后可评论
问答分类:
PHP
问答地址: