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

Din*_*esh 102 php arrays

我从postmeta获取post_id为:

$post_id = $wpdb->get_results("SELECT post_id FROM $wpdb->postmeta WHERE (meta_key = 'mfn-post-link1' AND meta_value = '". $from ."')");
Run Code Online (Sandbox Code Playgroud)

当我尝试print_r($post_id); 我有这样的数组:

Array
(
    [0] => stdClass Object
        (
            [post_id] => 140
        )

    [1] => stdClass Object
        (
            [post_id] => 141
        )

    [2] => stdClass Object
        (
            [post_id] => 142
        )

)
Run Code Online (Sandbox Code Playgroud)

我不知道如何遍历它,我怎么能得到像这样的数组

Array
(
    [0]  => 140


    [1] => 141


    [2] => 142

)
Run Code Online (Sandbox Code Playgroud)

知道我怎么能这样做.......提前谢谢

Ama*_*ali 219

最简单的方法是对对象进行JSON编码,然后将其解码回数组:

$array = json_decode(json_encode($object), True);
Run Code Online (Sandbox Code Playgroud)

或者如果您愿意,也可以手动遍历对象:

foreach ($object as $value) 
    $array[] = $value->post_id;
Run Code Online (Sandbox Code Playgroud)

演示!

  • 我知道为时已晚,但为什么你不使用类型转换......**(数组)$ obj** (6认同)
  • 为什么我们不能只执行 `$array = json_decode($object,true)` ? (3认同)
  • @akshaynagpal:它会导致错误,因为您将向一个函数提供一个对象,该函数需要一个JSON字符串作为其输入.在答案中,我将对象转换为JSON字符串,然后将其作为输入提供给json_decode(),以便它返回一个数组(第二个参数为True表示应该返回一个数组). (2认同)
  • @chhameed:如果您的对象内嵌套有其他对象,则类型转换将不起作用。请参阅示例 - https://eval.in/1124950 (2认同)

小智 56

非常简单,首先将对象转换为json对象,这会将对象的字符串返回给JSON代表.

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

$array = json_decode(json_encode($oObject),true);
Run Code Online (Sandbox Code Playgroud)


Ale*_*eri 20

试试这个:

$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;
    }
}
Run Code Online (Sandbox Code Playgroud)


小智 13

您可以将std对象转换为数组,如下所示:

$objectToArray = (array)$object;
Run Code Online (Sandbox Code Playgroud)

  • 这很棒,但它只转换第一级。如果您有嵌套,则必须对所有节点进行嵌套。 (4认同)

小智 7

$array = (array)$class; 
Run Code Online (Sandbox Code Playgroud)

  • 欢迎来到SO.您是否介意稍微扩展您的答案以解释它如何解决问题? (5认同)

Vla*_*lad 6

$wpdb->get_results("SELECT ...", ARRAY_A);
Run Code Online (Sandbox Code Playgroud)

ARRAY_A是"output_type"参数.它可以是四个预定义常量之一(默认为OBJECT):

OBJECT - result will be output as a numerically indexed array of row objects.
OBJECT_K - result will be output as an associative array of row objects, using first columns values as keys (duplicates will be discarded).
ARRAY_A - result will be output as an numerically indexed array of associative arrays, using column names as keys.
ARRAY_N - result will be output as a numerically indexed array of numerically indexed arrays.  
Run Code Online (Sandbox Code Playgroud)

请参阅:http://codex.wordpress.org/Class_Reference/wpdb


NJI*_*dar 6

在将STD类对象转换为array时,通过使用php的数组函数将对象转换为数组.

试试下面的代码片段.

/*** cast the object ***/    
foreach($stdArray as $key => $value)
{
    $stdArray[$key] = (array) $value;
}   
/*** show the results ***/  
print_r( $stdArray );
Run Code Online (Sandbox Code Playgroud)


Tay*_*yat 6

有两种简单的方法可以将 stdClass 对象转换为数组

$array = get_object_vars($obj);
Run Code Online (Sandbox Code Playgroud)

另一个是

$array = json_decode(json_encode($obj), true);
Run Code Online (Sandbox Code Playgroud)

或者您可以简单地使用 foreach 循环创建数组

$array = array();
foreach($obj as $key){
    $array[] = $key;
}
print_r($array);
Run Code Online (Sandbox Code Playgroud)