2017-04-11 54 views
0

我是新的多維數組在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個帖子
+0

[呦耶!(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

+0

@mkaatman笑@ meme。可能不是我想要的,但這是一個嘗試,你的鏡頭是什麼? :D –

+0

嘗試類似於:'$ things ['fruit'] ='orange'; $ things ['furniture'] ='table'; print_r($ things);' – mkaatman

回答

4
<?php 

class User { 

    public $id; 
    public $numPosts; 
    public $languages = array(); 

    public function __construct($id, $numPosts, $lang = array()){ 
     $this->id = $id; 
     $this->numPosts = $numPosts; 
     $this->languages = $lang; 
    } 
} 

$users = array(); 

$john = new User(1, 4, array("English", "French")); 
$fred = new User(2, 3, array("English")); 
$dave = new User(3, 7, array("German", "French", "Spanish")); 

$users[] = $john; 
$users[] = $fred; 
$users[] = $dave; 

$langPostCount = array(); 
$langUserCount = array(); 

foreach($users as $user){ 
    foreach($user->languages as $lang){ 
     $langUserCount[$lang] += 1; // this is what you already have from $count_values 
     //$langPostCount[$lang] += $user->numPosts; // can be done here but we'll do another loop 
    } 
} 

/* 
* the following can be done in the above loop, but you already have that functionality in your code 
* just need to do another loop through your languages, tallying the number of posts in that language 
* keep in mind this is not entirely accurate as your users have multiple languages. they might have 
* one post in english and 4 in french. A better way to do this would be to select the number of posts 
* in each language directly from the posts database. 
*/ 

foreach($langUserCount as $lang => $userCount){ 
    foreach($users as $user){ 
     if(in_array($lang, $user->languages)){ 
      $langPostCount[$lang] += $user->numPosts; 
     } 
    } 
} 

echo "<ul>"; 
foreach($langUserCount as $lang => $userCount){ 
    echo "<li>$userCount $lang with " . $langPostCount[$lang] . " posts.</li>"; 
} 
echo "</ul>"; 

?> 

OUTPUT

  • 2英7個職位。
  • 2個法文,共有11個帖子。
  • 1個德語,7個帖子。
  • 1西班牙語,7帖子。

正如你所看到的,並不完全準確。通過查詢你的帖子數據集,你最好通過自下而上的方式來獲取帖子數。

嘗試這個

在頂部添加新的理貨foreach循環,並在年底改變了UL循環。

$postsPerLanguage = array(); // add this 

foreach ($blogusers as $user) { 

    $descTokens = explode(',', $user->user_description); 
    ... 
    $numPosts = count_user_posts($the_user_id); 
    ... 
    // add this loop 
    foreach($descTokens as $lang){ 
     $postsPerLanguage[$lang] += $numPosts; 
    } 
} 

... 

<ul> 
    <?php 
     foreach ($count_values as $key=>$count) { 
      echo '<li>'.$key.' '.$count.' with '. $postsPerLanguage[$key] .' posts</li>'; 
     } 
    ?> 
</ul> 
+0

讓我們[在聊天中繼續討論](http://chat.stackoverflow.com/rooms/141395/discussion-between-rob-m-and-nospi)。 –

0

您的輸出不需要多維數組,你可以這樣實現它:

$final_array = array('Fruits'=> 'orange', 'Furniture'=> 'table') 

但例如,如果您有多個水果或傢俱,你可以做這樣的事情:

$final_array = array('Fruits'=> array('orange', 'apple'), 'Furniture'=> array('table', 'sofa')) 

,你可以這樣訪問蘋果:

echo $final_array['Fruits'][1]; 

print_r($final_array),我們有這樣的:

[Fruits] => (
    [0] => orange, 
    [1] => apple 
) 
[Furniture] => (
    [0] => table, 
    [1] => sofa 
)