2015-03-02 95 views
1

我的數組返回此創建一個數組:與特定領域

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)) 

則無需使用array_column創建只有「塞」中的一個數組?

+0

可否請您編輯代碼幾乎是不可讀的,把它放在多行至少。 – 2015-03-02 19:04:59

回答

2

使用foreach來遍歷外部數組,然後直接索引內部數組。

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

這應該與array_map工作:

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