我是新的多維數組在PHP中,我讀這SO answer,我試圖創建我的二維數組,但我怎麼輸出它?如何輸出多維數組?
$nPost = array("orange, table");
$count_values = array("fruit, forniture");
$final_array = array(array($count_values), array($nPost));
輸出將必須是:
Fruits: orange, Forniture: table
試過
print_r($final_array)
;
,但我得到
Array ([0] => Array ([0] => Array ([0] => fruit, forniture)) [1] => Array ([0] => Array ([0] => orange, table)))
0 fruit, forniture
UPDATE
現實生活的完整代碼(代碼中的註釋說明):
<?php
$stack = array();
$userID = array();
$nPost = array();
$blogusers = get_users('orderby=nicename&role=author');
foreach ($blogusers as $user) {
// get the language list for each user, and push to array
$descTokens = explode(',', $user->user_description);
$stack = array_merge($stack, $descTokens);
// get the ID for each user, and push to the array
// get the number of posts for each user ID and push to array
$the_user_id = $user->ID;
$numPosts = count_user_posts($the_user_id);
array_push($userID, $the_user_id);
array_push($nPost, $numPosts);
}
// get the count for each language by counting the duplicate strings
$count_values = array();
foreach ($stack as $a) {
@$count_values[$a]++;
}
$total_duplicates = 0;
foreach ($count_values as $a) {
if($count_values[$a]<=1){
unset($count_values[$a]);
} else{
$total_duplicates += $count_values[$a];
}
}
for($i = 0; $i < count($count_values); $i++){
$final_array[$count_values[$i]] = $nPost[$i];
}
foreach($final_array as $label => $item){
echo "$label: $item, ";
}
?>
// This gives me a correct result but not the n. posts
<ul>
<?php
foreach ($count_values as $key=>$count) {
echo '<li>'.$key.' '.$count.'</li>';
}
?>
</ul>
我們努力實現的是:
- 1法語用2個帖子
- 3英語用5個帖子
[呦耶!(https://cdn.meme。am/instances/500x/66975881/yo-dawg-yo-dawg-i-heard-you-liked-arrays-so-we-put-an-array-inside-your-array.jpg)對於初學者:'$ final_array = array($ count_values,$ nPost);'這可能還不是你想要的。 – mkaatman
@mkaatman笑@ meme。可能不是我想要的,但這是一個嘗試,你的鏡頭是什麼? :D –
嘗試類似於:'$ things ['fruit'] ='orange'; $ things ['furniture'] ='table'; print_r($ things);' – mkaatman