2017-02-22 27 views
0

我想從使用array_column wp_get_post_terms提取term_ids數組,但它不會返回任何東西。WordPress的array_column不工作wp_get_post_terms

下面是代碼:

$atype = wp_get_post_terms($property_ID, 'property_category', true); 
$types = array_column($atype, 'term_id'); // Not working so using array map 
$types2 = array_map(function($te) { 
    return $te->term_id; 
}, $atype); 

echo '$atype: '; 
echo '<pre>'; print_r($atype); echo '</pre>'; 
echo '$types: '; 
echo '<pre>'; print_r($types); echo '</pre>'; 
echo '$types2: '; 
echo '<pre>'; print_r($types2); echo '</pre>'; 

下面是結果:

$atype: Array 
(
    [0] => WP_Term Object 
    (
     [term_id] => 51 
     [name] => Office 
     [slug] => office 
     [term_group] => 0 
     [term_taxonomy_id] => 51 
     [taxonomy] => property_category 
     [description] => 
     [parent] => 0 
     [count] => 1 
     [filter] => raw 
    ) 

) 
$types: Array 
(
) 
$types2: Array 
(
    [0] => 51 
) 

爲什麼不array_column工作?根據文檔,它應該。我正在使用> php 5.5。 http://php.net/manual/en/function.array-column.php

回答

1

這是因爲對象 S,而不是2維陣列的wp_get_post_terms返回和陣列。 array_column只適用於php 7.0.0之前的二維數組。