创建具有特定字段的数组

时间:2015-03-02 19:01:29

标签: php arrays wordpress

我的数组返回:

Array ([0] => stdClass Object ([term_id] => 44 
                               [name] => Escritórios comerciais 
                               [slug] => escritorios-comerciais 
                               [term_group] => 0 [term_order] => 1 
                               [term_taxonomy_id] => 44 
                               [taxonomy] => area 
                               [description] => 
                               [parent] => 33 
                               [count] => 0) 
       [1] => stdClass Object ([term_id] => 45 
                               [name] => Escritórios de advocacia 
                               [slug] => escritorios-de-advocacia 
                               [term_group] => 0 
                               [term_order] => 2 
                               [term_taxonomy_id] => 45 
                               [taxonomy] => area 
                               [description] => 
                               [parent] => 33 
                               [count] => 0) 
       [2] => stdClass Object ([term_id] => 46 
                               [name] => Sede administrativa 
                               [slug] => sede-administrativa 
                               [term_group] => 0 
                               [term_order] => 3 
                               [term_taxonomy_id] => 46 
                               [taxonomy] => area 
                               [description] => 
                               [parent] => 33 
                               [count] => 0) 
       [3] => stdClass Object ([term_id] => 47 
                               [name] => Alimentação 
                               [slug] => alimentacao 
                               [term_group] => 0 
                               [term_order] => 4 
                               [term_taxonomy_id] => 47 
                               [taxonomy] => area 
                               [description] => 
                               [parent] => 33 
                               [count] => 0) 
       [4] => stdClass Object ([term_id] => 48 
                               [name] => Indústria 
                               [slug] => industria 
                               [term_group] => 0 
                               [term_order] => 5 
                               [term_taxonomy_id] => 48 
                               [taxonomy] => area 
                               [description] => 
                               [parent] => 33 
                               [count] => 0 )) 

只能使用' slug'创建一个数组。不使用array_column的字段?

2 个答案:

答案 0 :(得分:2)

使用foreach迭代外部数组,然后直接索引内部数组。

$slugs = array();
foreach($array as $key => $value) {
    $slugs[$key] = $value->slug;
}

答案 1 :(得分:1)

这适用于array_map

$result = array_map(function($v) { return $v->slug; }, $array);