我有以下动态生成的数组。数组值每次都可以更改,但位置保持不变(即名称将始终是每个数组的第2项)。
我如何回应每个阵列中相同的特定线?例如,假设我想让所有名字一起回响?
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将始终是第一个,名称将始终是第二个,依此类推。
这已经让我至少躲过了一个小时,这让我非常沮丧:(
答案 0 :(得分:3)
echo implode(", ", array_map(function(WP_Term $term){
return $term->name;
}, $array));
答案 1 :(得分: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++;
}
答案 2 :(得分:0)
例如,您的数组名称是$ items。
Foreach($items as item){
Echo $item->name;
}
答案 3 :(得分:0)
使用此
$cats = $cats = get_the_terms($post->ID, 'themes_categories');
foreach ($cats as $key => $object) {
echo $object->name;
}