drnx3715 2017-04-11 02:16
浏览 93
已采纳

如何输出多维数组?

I am new to multidimensional array in php, I read this SO answer and I tried to create my bidimensional array but how do I output it?

$nPost = array("orange, table");
$count_values = array("fruit, forniture");
$final_array = array(array($count_values), array($nPost));

Output would have to be:

Fruits: orange, Forniture: table

Tried

print_r($final_array);

But i got

Array ( [0] => Array ( [0] => Array ( [0] => fruit, forniture ) ) [1] => Array ( [0] => Array ( [0] => orange, table ) ) )
0 fruit, forniture

UPDATE

Real life full code is (explanation in code comments):

    <?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>

What we're trying to achieve is:

  • 1 French with 2 posts
  • 3 English with 5 posts
  • 写回答

2条回答 默认 最新

  • douque2016 2017-04-11 02:29
    关注
    <?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 English with 7 posts.
    • 2 French with 11 posts.
    • 1 German with 7 posts.
    • 1 Spanish with 7 posts.

    As you can see, not entirely accurate. You're better off getting post count by querying your posts dataset than by working from the bottom up.

    Try This

    Adds a new tally to the foreach loop at the top, and changes the ul loop at the end.

    $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>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog