0
我有一個wordpress foreach循環,它獲取類別名稱。數組中的下一項 - foreach循環
$i = 0;
foreach($catnamea as $legname) {
$i++;
echo $legname[$i].'<br>';
}
,如果我去$ legname [0] - 它會給我一個類別名稱,並作爲我了numeber它列出了所有其他名稱。但是,像這樣的事情發生時,我不得不輸入另一個數字,pertty很多,我希望它在每個項目的變量$ i上添加一個數字。
以下是完整的代碼,所以你可以看到什麼在
var pieData = [
<?php
$user_id = get_query_var('author');
$rand = array('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f');
//get all posts from author
$args = array(
'post_type' => 'post',
'author'=> $queried_object->ID
);
$the_query = new WP_Query($args);
if ($the_query->have_posts()) :
while ($the_query->have_posts()) : $the_query->the_post();
//put categories in array
$cat = get_the_category(get_the_ID());
$terms[] = $cat[0]->term_id;
$catnames[] = $cat[0]->cat_name;
endwhile;
wp_reset_query();
endif;
//count matching categories (array vals)
$countVal = array_count_values($terms);
$colors = array();
$catnames[] = array();
foreach($countVal as $count){
$color = '#'.$rand[rand(0,15)].$rand[rand(0,15)].$rand[rand(0,15)].$rand[rand(0,15)].$rand[rand(0,15)].$rand[rand(0,15)];
echo " {
value: ".$count.",
color:'".$color."'
},";
$colors[] = $color;
$catnamea[] = $catnames;
}
?>
]
var myPie = new Chart(document.getElementById("piec").getContext("2d")).Pie(pieData);
</script>
<?php
foreach($colors as $scolor) {
echo '<div style="background:'.$scolor.';height:5px; width:5px;float:left"></div>';
}
$i = 0;
foreach($catnamea as $legname) {
$i++;
echo $legname[$i].'<br>';
} ?>
你想沒有'$ i ++'呢? – DontVoteMeDown
目前$ i ++是越野車,不能正常工作,不止一次拉同一結果 – Zackskeeter
現在這很奇怪,考慮到''我可能永遠不會重複。你檢查過你的陣列了嗎? – DontVoteMeDown