1

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.

3 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

Before the loop add:

$books[$userID] = array();

Inside the loop use:

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

Comments

0

Try with this :

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

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.