views:

174

answers:

2

The purpose of this question is to find the best way to print data out of PHP multidimensional arrays.

How can you complete the following procedure below?

I have the following arrays

array1['id']['title']

and

array2['id']['tags'][]

The arrays have been generated by the function pg_fetch_array. This allows you refer to each value of element by its name or by its key.

Procedure to get the titles for questions and their tags

I would like to do the following

  1. First loop
    1. print the title from array1[$question_id]
    2. print all tags from array2[$question_id][] for the given question_id
  2. Second loop
    1. do the same as in 1.1 for the next question_id in the list
    2. do the same as in 1.2 for the next question_id in the list ...
  3. Continue this for all $question_ids in the list

I have used various methods unsuccessfully to complete the procedure

  1. to create a multidimensional array such that I can iterate though all items in a singe -foreach: merge_unique is not enough here. Other merges also remove one column which I do not want.
  2. to solve the problem with the two given arrays by while and foreach -sentences: I get 9 iterations for 3 questions as I have a foreach -clause inside a while -loop
+1  A: 

Foreword: Any of the following examples should give the expected result. They just get more complicated as you go down the page, and each has its own benefits.

First, the barebones of the function. You loop through array1, and print out the title. Then you go and grab the array from array2 that has the same id as the one we are currently looking at, loop through each value, and print the value.

foreach($array1 as $id => $sub_array)
{
    echo $sub_array['title'];
    foreach($array2[$id]['tags'] as $tag)
    {
        echo $tag;
    }
}


Now for a little clearer one:

 // Go through each question in the first array
 // ---$sub_array contains the array with the 'title' key
 foreach($array1 as $id => $sub_array)
 {
     // Grab the title for the first array
     $title = $sub_array['title'];

     // Grab the tags for the question from the second array
     // ---$tags now contains the tag array from $array2
     $tags = $array2[$id]['tags'];

     // 1.1 Print the Title
     echo $title;

     // 1.2 Go through each tag
     foreach($tags as $tag)
     {
         echo $tag;
     }
 }

It does a few more things than it needs to, but the added steps make it more clear.


And Just because I love making things more complicated, you could better seperate everything by letting functions handle Title/Tag Creation, and it would create less clutter in your foreach loop, which also means less frustration.

// Go through each question in the first array
foreach($array1 as $id => $sub_array)
{
    // Grab the title for the first array
    $title = $sub_array['title'];

    // Grab the tags for the question from the second array
    $tags = $array2[$id]['tags'];

    // 1.1 Print the Title & 1.2 Print the Tags
    create_question($title, $tags);
}

// Functions

// Create all the parts of a question.
function create_question($title, $tags)
{
    create_title($title);
    create_tags($tags);
}

// Print the Title
function create_title($title)
{
    echo $title;
}

// Loop Through Each Tag and Print it
function create_tags($tags)
{
    echo "<ul>";
    foreach($tags as $tag)
    {
        echo "<li>".$tag."</li>";
    }
    echo "</ul>";
}
Chacha102
And that'd be why you're listed twice on the stats page... (http://meta.stackoverflow.com/questions/14679/user-duplicated-in-stats-page) =)
David Thomas
I try :)
Chacha102
This is an excellent answer. - Thank you for taking functions, since they separate your logic nicely.
Masi
**How should I compile my data from my database such that I can use your arrays?** Perhaps, by `pg_fetch_array`?
Masi
This `create_question($title);` should apparently be `create_question($title, $tags);`
Masi
**How should I use your code with functions correctly?** - I just replaced your nice names with my long names. However, I guess that I should not do that, but otherwise define the variables.
Masi
1) PG_Fetch_Array will get individual Database Records. You then just have to sort the titles/tags by question ID. 2) Yes. 3) As long as you change the variable/function names **everywhere** it should work fine.
Chacha102
**Do you mean that I need to use PHP's sort function to sort titles and tags by question ID?** - Or what do you mean by sorting titles/tags? - Please, see the bug fix which I made to your code.
Masi
I am new in PHP. **How should I prepare data for your functions?**
Masi
The `$title` should only be the actual string that is the title, not an array. The `$tags` should be a single, non-multidimensional array containing tag names.
Chacha102
I get `invalid argument supplied`in this `foreach($tags as $tag)` which is the `create_tags` -function. - I have the functions in a separate file as the main function. I include the functions by `require` to the main function. **How should I store your code, in separate files or all in one?**
Masi
1) You need to make sure that you are passing an **Array** to the `create_question` and `create_tags` function. It should be an **Array** of tags like: `$tags = array('tag1', 'tag2', 'tag3');`. It shouldn't matter about where you store the code either. But you need to make sure that the second parameter you pass to `create_question` is an **Array**.
Chacha102
It seems the problem we keep having (same as the last problem) is the whole array part, and getting the correct values passed. If you need to, print_r the entire array you are sending in right before you send it. Make sure it looks like what you actually want to send in.
Chacha102
@Cha: I can now pass the $tags as an array: http://dpaste.com/81734/ to the function. However, I have not managed to a string for $title. **This may be a bug in the code**, since I get this http://dpaste.com/81737/ when I have this http://dpaste.com/81740/
Masi
Change `$title = $titles_and_Qid['title'];` to `$title = $titles_and_Qid['title']['title']`;
Chacha102
@Cha: I get the correct data out of the second while -loop now. The problem is now only in the foreach -loop which I do not understand completely. This is the output of the while -loop and the code for foreach http://dpaste.com/81929/
Masi
`$title = $titles [ $titles_and_Qid['question_id'] ];` should be` $title = $titles [ $titles_and_Qid['question_id'] ]['title'];`
Chacha102
I got it work! - I needed to the same source of the question IDs in getting the title as in getting the tags. **Thank you for your help!** - This is the working code http://dpaste.com/81933/
Masi
+1  A: 

I'm sure I am missing somethnig here, but...

foreach($array1 as $id => $title) {
  echo $title['title'];
  foreach($array2[$id]['tags'] as $tag) {
    echo $tag;
  }
}
Zed