Take the tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I'm trying to generate a multidimensional array like this in PHP :

$books =  array(

    "8" => array(   "my girl" => 2.5, 
                    "the god delusion" => 3.5,
                    "tweak" => 3, "the shack" => 4,
                    "the birds in my life" => 2.5,
                    "new moon" => 3.5),

    "14" => array(    "the last lecture" => 2.5, 
                      "the god delusion" => 3.5,
                      "the noble wilds" => 3, "the shack" => 3.5,
                      "the birds in my life" => 2.5, "new moon" => 1)
     );

from a database table which is like this :

ID  value   title
------------------------------------------------------------------
8   5   Clara Callan
8   5   Where You'll Find Me: And Other Stories
8   5   The Middle Stories
8   5   Jane Doe
8   6   The Witchfinder (Amos Walker Mystery Series)
8   6   More Cunning Than Man: A Social History of Rats an...
8   7   Goodbye to the Buttermilk Sky
9   6   Beloved (Plume Contemporary Fiction)
12  10  If I'd Known Then What I Know Now: Why Not Learn f...
14  5   Mary-Kate & Ashley Switching Goals (Mary-Kate ...
14  5   Tell Me This Isn't Happening
14  6   Flood : Mississippi 1927
16  9   Airframe
17  7   Death in the Clouds
17  5   Bant/Spec.Last of the Breed
17  6   Piercing the Darkness
17  3   Prophet
19  7   Prague : A Novel

I'm already tried several ways but I still can't figure out how to do exactly like that. I've been searching numerous threads in here but still no one discussing about this. I'm a newbie in PHP, so I don't understand too much array concept in PHP. Currently my PHP code is like this :

        $result = mysql_query($mySelectQuery) or die("<br/><br/>".mysql_error());
        $books = array();
        while ($row = mysql_fetch_array($result)) 
        {
            $userID = $row{'User-ID'};
            $books[$userID] = array($row{'Book-Title'} => $row{'Book-Rating'},);
        }

That code already produce similar result with "what I want", but it's still replace the existing book records, so in the end every user only have one book record in their array. My question is:

How could I populate a multidimensional array formatted like mine with the result of my query?

Thanks a million in advance for your answers. And sorry for bad English.

share|improve this question
add comment

3 Answers

up vote 1 down vote accepted

Try:

$result = mysql_query($mySelectQuery) or die("<br/><br/>".mysql_error());
    $books = array();
    while ($row = mysql_fetch_array($result)) 
    {
        $userID = $row{'User-ID'};
        $books[$userID][$row{'Book-Title'}] = $row{'Book-Rating'};
    }

This will assign your book title as a array key/index, and set the rating as it's value.

share|improve this answer
 
Thanks, it work ! –  Herdi Naufal Mar 13 at 12:05
add comment

Before the loop add:

$books[$userID] = array();

Inside the loop use:

$books[$userID][] = array($row{'Book-Title'} => $row{'Book-Rating'},);

share|improve this answer
add comment

Try with this :

$books[$userID][] = array($row{'Book-Title'} => $row{'Book-Rating'},);

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.