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 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 求螺旋焊缝的图像处理
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面
  • ¥15 itunes恢复数据最后一步发生错误
  • ¥15 关于#windows#的问题:2024年5月15日的win11更新后资源管理器没有地址栏了顶部的地址栏和文件搜索都消失了