【问题标题】:Create an array with a specific field创建具有特定字段的数组
【发布时间】:2015-05-03 06:11:35
【问题描述】:

我的数组返回这个:

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 创建数组?

【问题讨论】:

  • 能否请您编辑几乎无法阅读的代码,至少放在多行上。

标签: php arrays wordpress


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

    这应该适用于array_map

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

    【讨论】:

      猜你喜欢
      • 2021-10-02
      • 2015-04-11
      • 2012-08-08
      • 2022-11-03
      • 1970-01-01
      • 1970-01-01
      • 2013-03-07
      • 1970-01-01
      • 2020-03-22
      相关资源
      最近更新 更多