【问题标题】:How to output a multidimensional array?如何输出多维数组?
【发布时间】:2017-04-11 02:16:28
【问题描述】:

我是 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

更新

现实生活中的完整代码是(代码 cmets 中的解释):

    <?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 篇文章

【问题讨论】:

  • Yo dawg! 对于初学者:$final_array = array($count_values, $nPost); 这可能仍然不是你想要的。
  • @mkaatman 哈哈@模因。可能不是我想要的,但这是一个尝试,你的镜头是什么? :D
  • 尝试类似:$things['fruit'] = 'orange';$things['furniture'] = 'table';print_r($things);
  • 你像一个没有键的字符串(“橙色,表格”)一样发送水果和橙色,而 php 给出了默认的数字键,从 0 开始,这就是你得到[0] =&gt; fruit, forniture
  • @mkaatman 是的,我们手动为每个值设置了 1 个值,但是如果我们有一个字符串列表(实际上是动态的)呢?

标签: php


【解决方案1】:
<?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>";

?>

输出

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

【讨论】:

【解决方案2】:

您的输出不需要多维数组,您可以这样实现:

$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
)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-10-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多