0

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
7
  • Yo dawg! For starters: $final_array = array($count_values, $nPost); It's probably still not what you want. Commented Apr 11, 2017 at 2:27
  • @mkaatman lol @ the meme. Probably not what I want but it's a try, what's your shot? :D Commented Apr 11, 2017 at 2:28
  • Try something like: $things['fruit'] = 'orange';$things['furniture'] = 'table';print_r($things); Commented Apr 11, 2017 at 2:29
  • you send fruit and orange like one string ("orange, table") with no key and php give the default key which is numeric and started from 0 that's why you get [0] => fruit, forniture Commented Apr 11, 2017 at 2:33
  • @mkaatman yeah ok, we manually set 1 single value for each, but what if we have a list of strings (which in reality are dynamic) ? Commented Apr 11, 2017 at 2:34

2 Answers 2

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 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>
Sign up to request clarification or add additional context in comments.

1 Comment

0

Your output doesn't need multidimensional array you can achieve it like this:

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

but for example if you have multiple fruits or furniture you can make something like this:

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

and you can access apple like this:

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

and for print_r($final_array) we have this:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.