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


Convert stdClass object to array in 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
)

知道我该怎么做吗?

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

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

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

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

很简单,首先将你的对象变成一个 json 对象,这会将你的对象的字符串返回到一个 JSON 代表。

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

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

试试这个:

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

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

$objectToArray = (array)$object;

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

$array = get_object_vars($obj);

其他是

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

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

$array = array();
foreach($obj as $key){
    $array[] = $key;
}
print_r($array);

对于一维数组:

$array = (array)$class; 

对于多维数组:

function stdToArray($obj){
  $reaged = (array)$obj;
  foreach($reaged as $key => &$field){
    if(is_object($field))$field = stdToArray($field);
  }
  return $reaged;
}

将 STD 类对象转换为数组时。使用 php 的数组函数将对象强制转换为数组

尝试使用以下代码片段。

/*** cast the object ***/    
foreach($stdArray as $key => $value)
{
    $stdArray[$key] = (array) $value;
}   
/*** show the results ***/  
print_r( $stdArray );
$wpdb->get_results("SELECT ...", ARRAY_A);

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.  

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

你可以试试这个:

$aInitialArray = array_map(function($oObject){
    $aConverted = get_object_vars($oObject);
    return $aConverted['post_id'];
}, $aInitialArray);

如果你有一个数组并且数组元素是stdClass项目,那么这就是解决方案:

foreach($post_id as $key=>$item){
    $post_id[$key] = (array)$item;
}

现在stdClass已被替换为数组内的数组作为新的数组元素

使用 Std 中的 ArrayObject 或构建自己的

(new ''ArrayObject($existingStdClass))

可以在新类上使用 build in 方法:

getArrayCopy()

或将新对象传递给

iterator_to_array

假设 $post_id 是$item数组

$post_id = array_map(function($item){
       return $item->{'post_id'};
       },$post_id);

强文本

我有一个函数myOrderId($_GET['ID']);它返回多维 OBJ. 作为字符串

其他 1 班轮都没有为我醒来。

这两者都有效:

$array = (array)json_decode(myOrderId($_GET['ID']), True);
$array = json_decode(json_decode(json_encode(myOrderId($_GET['ID']))), True);

我对此有问题,因为我的stdClass中反复有stdClasses。此函数递归地将所有元素转换为数组:

$newArray = objectToArray($oldArray)
function objectToArray($data) {
    // If the element being looked is an object convert to an array
    if(is_object($data)) {
      $data = get_object_vars($data);
    }
    // If the element is an array, iterate though it and call the function again on each item
    if(is_array($data)) {
        foreach($data as $key=>$value){
            $data[$key] = objectToArray($value);
        }
    }
    return $data;
}