我从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)
小智 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)
小智 7
$array = (array)$class;
Run Code Online (Sandbox Code Playgroud)
$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
在将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)
有两种简单的方法可以将 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)
| 归档时间: |
|
| 查看次数: |
238382 次 |
| 最近记录: |