如何合并相同的数组键,但数值不同。
问题描述:
如何合并相同的数组键,但数值不同。 我用我的代码输出值如下:如何合并相同的数组键,但数值不同。
西甲Division2016-17,2017-18国王杯Ray2016-17
但是当我回声出我的代码,然后我得到输出如下:
西甲Division2017-18国王杯Ray2016-17西甲 Division2016-17
你能看两次西班牙西甲赛区吗?我想这一次,但这一年两次。 代码:
<?php
$champion_team = get_post_meta(get_the_ID(), 'football_league_team_name', true);
$terms_competition = get_the_terms(get_the_ID(), 'competition');
$terms_session = get_the_terms(get_the_ID(), 'session');
$cc= array_merge($terms_competition, $terms_session);
foreach ($cc as $c) {
# code...
echo $c->name;
}
?>
print_r的输出:
WP_Term Object ([term_id] => 6 [name] => Spanish Primera Division [slug] => spanish-primera-divisioj [term_group] => 0 [term_taxonomy_id] => 6 [taxonomy] => competition [description] => [parent] => 0 [count] => 10 [filter] => raw)
WP_Term Object ([term_id] => 7 [name] => 2017-18 [slug] => 2017-18 [term_group] => 0 [term_taxonomy_id] => 7 [taxonomy] => session [description] => [parent] => 0 [count] => 4 [filter] => raw)
WP_Term Object ([term_id] => 18 [name] => Copa Del Ray [slug] => copa-del-ray [term_group] => 0 [term_taxonomy_id] => 18 [taxonomy] => competition [description] => [parent] => 0 [count] => 1 [filter] => raw)
WP_Term Object ([term_id] => 11 [name] => 2016-17 [slug] => 2016-17 [term_group] => 0 [term_taxonomy_id] => 11 [taxonomy] => session [description] => [parent] => 0 [count] => 9 [filter] => raw)
WP_Term Object ([term_id] => 6 [name] => Spanish Primera Division [slug] => spanish-primera-divisioj [term_group] => 0 [term_taxonomy_id] => 6 [taxonomy] => competition [description] => [parent] => 0 [count] => 10 [filter] => raw)
WP_Term Object ([term_id] => 11 [name] => 2016-17 [slug] => 2016-17 [term_group] => 0 [term_taxonomy_id] => 11 [taxonomy] => session [description] => [parent] => 0 [count] => 9 [filter] => raw)
答
只需添加他们
$cc= $terms_competition + $terms_session;
不存在于$terms_competition
中的任何密钥都是从$terms_session
开始添加的。
答
You can try like this :
With array_unique
foreach (array_unique($data) as $d) {
// Do stuff with $d ...
}
答
<?php
$champion_team = get_post_meta(get_the_ID(), 'football_league_team_name', true);
$terms_competition = get_the_terms(get_the_ID(), 'competition');
$terms_session = get_the_terms(get_the_ID(), 'session');
$cc= array_merge($terms_competition, $terms_session);
$temp = array();
foreach ($cc as $c) {
# code...
if(!in_array($temp)) {
echo $c->name;
$temp[] = $c->term_id;
}
}
+0
显示:警告:in_array()需要至少2个参数,1在C:\ wamp \ www \ wordpress \ wp-content \ themes \ testtheme \ framework \ team \ honour.php中给出,位于第48行 –
谢谢。通过这种方式? –