将对象转换为数组

I am working with WordPress and since I don't believe it is possible to sort object details, I was wondering how to go about converting my Object to an Array, so that sorting can be possible.

Any help or guidance would be greatly appreciated.

I am using the WP function get_categories();

The complete content of $category is:

$category->term_id
$category->name
$category->slug
$category->term_group
$category->term_taxonomy_id
$category->taxonomy
$category->description
$category->parent
$category->count
$category->cat_ID
$category->category_count
$category->category_description
$category->cat_name
$category->category_nicename
$category->category_parent
doudou8081
doudou8081 不,我的意思是你对象的结构是什么?它有什么数据?
大约 9 年之前 回复
dongzuoyue6556
dongzuoyue6556 你有什么对象?
大约 9 年之前 回复

7个回答

If the object is not too complex (in terms of nesting) you can cast the class to an array:

$example = new StdClass();
$example->foo = 'bar';

var_dump((array) $example);

outputs:

array(1) { ["foo"]=> string(3) "bar" } 

However this will only convert the base level. If you have nested objects such as

$example = new StdClass();
$example->foo = 'bar';
$example->bar = new StdClass();
$example->bar->blah = 'some value';

var_dump((array) $example);

Then only the base object will be cast to an array.

array(2) { 
  ["foo"]=> string(3) "bar" 
  ["bar"]=> object(stdClass)#2 (1) { 
    ["blah"]=> string(10) "some value" 
  }
} 

In order to go deeper, you would have to use recursion. There is a good example of an object to array conversion here.

duan_88598
duan_88598 当我发布我的功能时,我错过了你的链接,并不意味着踩到脚趾。 +1
大约 9 年之前 回复
dongyejun1983
dongyejun1983 谢谢! 这正是我所寻找的。 谢谢一堆安迪。
大约 9 年之前 回复



要将对象转换为数组,可以使用 get_object_vars()</ code>( PHP手册):</ p>

  $  categoryVars = get_object_vars($ category)
</ code> </ pre>
</ div>

展开原文

原文

To convert an object to array you can use get_object_vars() (PHP manual):

$categoryVars = get_object_vars($category)

doucheng1884
doucheng1884 虽然这会回答OP,但如果在基础级别有一个数组,然后在其中的对象或进一步递归,它将失败。
接近 4 年之前 回复



就像</ p>

  $ array =(array)$ object; 
一样简单 </ code> </ pre>

http://www.php.net/manual/en/language.types.array.php#language.types.array.casting </ p>
</ div>

展开原文

doushichi3678
doushichi3678 分2步完成
大约 9 年之前 回复
dstk51319
dstk51319 我试过$ array =(array)get_categories(); 但这不起作用。
大约 9 年之前 回复

To convert the entire object and all it's properties to arrays, you can use this clunky function I've had kicking around for a while:

function object_to_array($object)
{
    if (is_array($object) OR is_object($object))
    {
        $result = array(); 
        foreach($object as $key => $value)
        { 
            $result[$key] = object_to_array($value); 
        }
        return $result;
    }
    return $object;
}

Demo: http://codepad.org/Tr8rktjN

But for your example, with that data, you should be able to just cast to array as others have already said.

$array = (array) $object;
dongyuan9292
dongyuan9292 它应该工作正常,尝试首先将get_categories()分配给变量。
大约 9 年之前 回复
doucang6739
doucang6739 类型转换在这种情况下不起作用。
大约 9 年之前 回复

A less clunky way might be:

function objectToArray($object) 
{ 
    if(!is_object( $object ) && !is_array( $object ))
    { 
        return $object; 
    } 
    if(is_object($object) ) 
    { 
        $object = get_object_vars( $object ); 
    } 
    return array_map('objectToArray', $object ); 
}

(Sourced from http://www.sitepoint.com/forums/showthread.php?438748-convert-object-to-array) Note, if you'd like this as a method in a class, change the last line to:

return array_map(array($this, __FUNCTION__), $object ); 

To add to @galen

<?php 
    $categories = get_categories(); 
    $array = (array)$categories;  
?>
dtby67541
dtby67541 大家都知道,这种方法不适用于get_categories(); 我在@Andy发布的链接中使用了递归对象来进行数组转换
大约 9 年之前 回复



  $ array = json_decode(json_encode($ object),true); 
</ code> </ pre> \ n </ div>

展开原文

原文

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

doouzlrvb01417498
doouzlrvb01417498 嗨,你介意为其他程序员扩展答案吗?
4 年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐