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 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?