在PHP中将stdClass对象转换为数组
我从Posteta获取post_id,如下所示:在PHP中将stdClass对象转换为数组,php,arrays,Php,Arrays,我从Posteta获取post_id,如下所示: $post_id = $wpdb->get_results("SELECT post_id FROM $wpdb->postmeta WHERE (meta_key = 'mfn-post-link1' AND meta_value = '". $from ."')"); 当我尝试打印时($post\u id) 我有这样的数组: Array ( [0] => stdClass Object (
$post_id = $wpdb->get_results("SELECT post_id FROM $wpdb->postmeta WHERE (meta_key = 'mfn-post-link1' AND meta_value = '". $from ."')");
当我尝试打印时($post\u id)代码>
我有这样的数组:
Array
(
[0] => stdClass Object
(
[post_id] => 140
)
[1] => stdClass Object
(
[post_id] => 141
)
[2] => stdClass Object
(
[post_id] => 142
)
)
$objectToArray = (array)$object;
我不知道如何遍历它,我怎么能得到这样的数组
Array
(
[0] => 140
[1] => 141
[2] => 142
)
你知道我该怎么做吗?试试这个:
$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;
}
}
最简单的方法是对对象进行JSON编码,然后将其解码回数组:
$array = json_decode(json_encode($object), true);
或者,如果愿意,也可以手动遍历对象:
foreach ($object as $value)
$array[] = $value->post_id;
您可以尝试以下方法:
$aInitialArray = array_map(function($oObject){
$aConverted = get_object_vars($oObject);
return $aConverted['post_id'];
}, $aInitialArray);
数组是一个“输出类型”参数。它可以是四个预定义常量之一(默认为OBJECT):
请参阅:非常简单,首先将对象转换为json对象,这将把对象的字符串转换为json代表 获取该结果并使用一个额外的参数true进行解码,该参数将转换为关联数组
$array = json_decode(json_encode($oObject),true);
对于一维数组:
$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的array函数将对象转换为数组 使用以下代码段进行尝试
/*** cast the object ***/
foreach($stdArray as $key => $value)
{
$stdArray[$key] = (array) $value;
}
/*** show the results ***/
print_r( $stdArray );
如果您有一个数组,并且数组元素是
stdClass
项,那么这就是解决方案:
foreach($post_id as $key=>$item){
$post_id[$key] = (array)$item;
}
现在,
stdClass
已被替换为数组中的一个数组,作为新的数组元素使用Std中的ArrayObject或构建自己的数组
(新建\ArrayObject($existingStdClass))
您可以在新类上使用内置方法:
getArrayCopy()
或者将新对象传递给
迭代器到数组
假设$post_id是$item的数组
$post_id = array_map(function($item){
return $item->{'post_id'};
},$post_id);
strong text您可以将std对象转换为如下数组:
Array
(
[0] => stdClass Object
(
[post_id] => 140
)
[1] => stdClass Object
(
[post_id] => 141
)
[2] => stdClass Object
(
[post_id] => 142
)
)
$objectToArray = (array)$object;
我有一个函数
myOrderId($\u GET['ID'])返回多维对象的代码>。作为字符串
没有一个班轮为我工作
这两种方法都有效:
$array = (array)json_decode(myOrderId($_GET['ID']), True);
$array = json_decode(json_decode(json_encode(myOrderId($_GET['ID']))), True);
有两种简单的方法可以将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);
欢迎来到SO。你介意把你的答案扩大一点来解释它是如何解决这个问题的吗?对于一维数组:$array=(array)$class;对于多维数组:上面的代码为什么我们不能只执行$array=json\u decode($object,true)
?@akshaynagpal:这会导致一个错误,因为您将一个对象提供给一个需要json字符串作为其输入的函数。在回答中,我将对象转换为JSON字符串,然后将其作为输入提供给JSON_decode(),这样它将返回一个数组(第二个参数为True表示应该返回一个数组)。我知道这太晚了,但为什么不使用类型转换。。。(数组)$obj@NgSekLong:不太可能,不。@chhameed:Typecasting如果对象中嵌套了其他对象,则无法工作。请参见示例-将stdobject更改为arrayIf$existingStdClass
的完美函数,如果该属性是另一个stdClass
,则该属性在生成的数组中仍然是stdClass。如果您需要递归工作的东西,那么您似乎需要使用json技术。问题的可能副本是不可json编码或不标准化的值,例如日期。这将外部对象转换为数组,但是,如果其中任何属性也是对象,它们将不会被转换。根据OP的问题,他有一个层次的对象结构。对于下一个级别,您必须添加另一个foreach循环。这是WordPress world中唯一建议的方法。这很好,但它只转换第一个级别。如果有嵌套,则必须对所有节点进行嵌套。