我有以下动态生成的数组。数组值每次都可以更改,但位置保持不变(即名称始终是每个数组的第二项)。
我将如何从每个数组回显相同的特定行?例如,假设我想把所有的名字都呼应在一起?
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永远是第一位的,名字永远是第二位的,依此类推。
这使我至少躲了一个小时,这使我非常沮丧:
发布于 2016-01-02 13:11:19
echo implode(", ", array_map(function(WP_Term $term){
return $term->name;
}, $array));发布于 2016-01-02 13:12:16
您需要迭代数组以打印该对象的名称或任何其他属性。
$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++;
}发布于 2016-08-22 05:46:39
使用这个
$cats = $cats = get_the_terms($post->ID, 'themes_categories');
foreach ($cats as $key => $object) {
echo $object->name;
}https://stackoverflow.com/questions/34566244
复制相似问题