2016-01-02 70 views
0

我有以下動態生成的數組。數組值可以每次都改變,但是位置保持不變(即,名稱將始終是每個數組的第二項)。從每個陣列回聲特定行

我將如何迴應每個陣列中相同的特定行?比如說,我想讓所有的名字一起迴盪?

Array 
(
    [0] => WP_Term Object 
     (
      [term_id] => 437 
      [name] => 128 GB 
      [slug] => 128-gb 
      [term_group] => 0 
      [term_taxonomy_id] => 437 
      [taxonomy] => pa_phone-capacity 
      [description] => 
      [parent] => 0 
      [count] => 88 
      [filter] => raw 
     ) 

    [1] => WP_Term Object 
     (
      [term_id] => 19 
      [name] => AT&T 
      [slug] => att 
      [term_group] => 0 
      [term_taxonomy_id] => 19 
      [taxonomy] => pa_phone-carrier 
      [description] => 
      [parent] => 0 
      [count] => 288 
      [filter] => raw 
     ) 

    [2] => WP_Term Object 
     (
      [term_id] => 280 
      [name] => Flawless 
      [slug] => flawless 
      [term_group] => 0 
      [term_taxonomy_id] => 280 
      [taxonomy] => pa_phone-condition 
      [description] => Works perfectly. 

No noticeable flaws, still in its package or looks like new. 

Has zero scratches or scuffs. 
      [parent] => 0 
      [count] => 338 
      [filter] => raw 
     ) 

    [3] => WP_Term Object 
     (
      [term_id] => 442 
      [name] => iPhone 
      [slug] => iphone 
      [term_group] => 0 
      [term_taxonomy_id] => 442 
      [taxonomy] => pa_device 
      [description] => 
      [parent] => 0 
      [count] => 496 
      [filter] => raw 
     ) 

    [4] => WP_Term Object 
     (
      [term_id] => 284 
      [name] => iPhone 6 Plus 
      [slug] => iphone-6-plus 
      [term_group] => 0 
      [term_taxonomy_id] => 284 
      [taxonomy] => pa_phone-model 
      [description] => 
      [parent] => 0 
      [count] => 72 
      [filter] => raw 
     ) 

    [5] => WP_Term Object 
     (
      [term_id] => 443 
      [name] => Smartphone 
      [slug] => smartphone 
      [term_group] => 0 
      [term_taxonomy_id] => 443 
      [taxonomy] => pa_device 
      [description] => 
      [parent] => 0 
      [count] => 1352 
      [filter] => raw 
     ) 

我需要一個保持動態的解決方案。有時可能有8個術語對象,有時可能只有5個像上面的數組。 term_id將始終是第一個,名稱將永遠是第二個,等等。

這已經躲避現在我要至少一個小時,它的折騰我這麼多:(

回答

3
echo implode(", ", array_map(function(WP_Term $term){ 
    return $term->name; 
}, $array)); 
3

您需要遍歷數組打印的名稱或對象的任何其他財產。

$terms = get_terms(); 
$length = count($terms); 
$counter = 1; 
foreach($terms as $term) { 
    echo isset($term->name) ? $term->name : ''; 
    echo ($counter != $length) ? PHP_EOL : ''; //Any separator 
    $counter++; 
} 
0

例如您的數組名稱爲$項目。

Foreach($items as item){ 
     Echo $item->name; 
} 
0

使用本

$cats = $cats = get_the_terms($post->ID, 'themes_categories'); 
foreach ($cats as $key => $object) { 
echo $object->name; 
}