在 PHP 中将 stdClass 对象转换为数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19495068/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Convert stdClass object to array in PHP
提问by Dinesh
I fetch post_id from postmeta as:
我从 postmeta 获取 post_id 为:
$post_id = $wpdb->get_results("SELECT post_id FROM $wpdb->postmeta WHERE (meta_key = 'mfn-post-link1' AND meta_value = '". $from ."')");
when i try print_r($post_id);
I have array like this:
当我尝试时,print_r($post_id);
我有这样的数组:
Array
(
[0] => stdClass Object
(
[post_id] => 140
)
[1] => stdClass Object
(
[post_id] => 141
)
[2] => stdClass Object
(
[post_id] => 142
)
)
and i dont know how to traverse it, and how could I get array like this
我不知道如何遍历它,我怎么能得到这样的数组
Array
(
[0] => 140
[1] => 141
[2] => 142
)
Any idea how can I do this?
知道我该怎么做吗?
回答by Amal Murali
The easiest way is to JSON-encode your object and then decode it back to an array:
最简单的方法是对对象进行 JSON 编码,然后将其解码回数组:
$array = json_decode(json_encode($object), true);
Or if you prefer, you can traverse the object manually, too:
或者,如果您愿意,也可以手动遍历对象:
foreach ($object as $value)
$array[] = $value->post_id;
回答by Rey Ramos
Very simple, first turn your object into a json object, this will return a string of your object into a JSON representative.
很简单,先把你的对象转成json对象,这样就会把你的对象的字符串返回成一个JSON代表。
Take that result and decode with an extra parameter of true, where it will convert to associative array
获取该结果并使用额外的 true 参数进行解码,它将转换为关联数组
$array = json_decode(json_encode($oObject),true);
回答by Alessandro Minoccheri
Try this:
尝试这个:
$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;
}
}
回答by Dinesh Kaswan
You can convert an std object to array like this:
您可以像这样将 std 对象转换为数组:
$objectToArray = (array)$object;
回答by Stack Overflow
For one-dimensional arrays:
对于一维数组:
$array = (array)$class;
For multi-dimensional array:
对于多维数组:
function stdToArray($obj){
$reaged = (array)$obj;
foreach($reaged as $key => &$field){
if(is_object($field))$field = stdToArray($field);
}
return $reaged;
}
回答by NJInamdar
While converting a STD class object to array.Cast the object to array by using arrayfunction of php.
将 STD 类对象转换为数组时,使用php 的数组函数将对象转换为数组。
Try out with following code snippet.
尝试使用以下代码片段。
/*** cast the object ***/
foreach($stdArray as $key => $value)
{
$stdArray[$key] = (array) $value;
}
/*** show the results ***/
print_r( $stdArray );
回答by Vlad
$wpdb->get_results("SELECT ...", ARRAY_A);
ARRAY_A is a "output_type" argument. It can be one of four pre-defined constants (defaults to OBJECT):
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.
回答by Sajuna Fernando
You can try this:
你可以试试这个:
$aInitialArray = array_map(function($oObject){
$aConverted = get_object_vars($oObject);
return $aConverted['post_id'];
}, $aInitialArray);
回答by softnwords
if you have an array and array element is stdClass
item then this is the solution:
如果你有一个数组并且数组元素是stdClass
item 那么这是解决方案:
foreach($post_id as $key=>$item){
$post_id[$key] = (array)$item;
}
now the stdClass
has been replaced with an array inside the array as new array element
现在stdClass
已被替换为数组内的数组作为新的数组元素
回答by Decebal
Using the ArrayObject from Std or building your own
使用 Std 中的 ArrayObject 或构建自己的
(new \ArrayObject($existingStdClass))
(新的\ArrayObject($existingStdClass))
you can use the build in method on the new class:
您可以在新类上使用 build in 方法:
getArrayCopy()
getArrayCopy()
or pass the new object to
或将新对象传递给
iterator_to_array
迭代器到数组