4

Sorry for the beginners question.
I've searched for an hour now, and can only find info on adding 1 key => value inside the while loop. I'm aiming for this result. Thanks

$menu = array(  
    '1' => array('id' => 1, 'parentid' => 0, 'title' => 'Apple'),  
    '2' => array('id' => 2, 'parentid' => 0, 'title' => 'Banana'),  
    '3' => array('id' => 3, 'parentid' => 0, 'title' => 'Tangerine'),  
    '4' => array('id' => 4, 'parentid' => 3, 'title' => 'Pear')
);


I've tried a number of things but this seems to be the closest.

$menu = array();
while($row = mysql_fetch_array($query)) {
    $menu[] = $row['id'] ;
    $menu[] = $row['parentid'] ;
    $menu[] = $row['title'];
}
0

2 Answers 2

25

Ahh, looks like you want something like

$menu = array();
while ($row = mysql_fetch_array($query)) {
    $menu[] = array(
        "id" => $row['id'], 
        "parentid" => $row['parentid'], 
        "title" => $row['title']
    );
}

Associative array keys are created using "key" => "value".


Edit

Off topic a bit, but I'd strongly recommend learning PDO for your queries. It's really easy to learn and has a ton of strong points - security and flexibility being the most important - and really takes your scripts to the next level.

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

6 Comments

thanks for the speedy response. Only thing is that this puts the array inside an array. so now i have: Array ( [0] => Array ( [categoryid] => 160 [catparentid] => 93 [catname] => Cables ) [1] => Array ( [categoryid] => 136 [catparentid] => 98 [catname] => Holders ) [2] => Array ( etc.... Can i get it so it's just one array with the values? Cheers!
But...that's exactly what you said you wanted. :) Anyway, no, you can't have one array with all the values referenced using associative keys, because the values will overwrite as each new key is processed. That is, the "id" => 56 will be replaced by "id"=> 57 and so on, and at the end you'll have one array with three associative keys, holding the last set of values. Or, if you dump all the values into an array with no associative keys, you'll have a big pile of unorganized values - also not good :)
sorry. complete overlooked that. you are right, that's what i asked for! Sorry. 1.5 months in and i'm still getting the basics sorted. thanks again!
No worries, good luck. And have a look at PDO - did I mention it's easy? :)
Yeah, it adds a layer of security against SQL injection (big topic, not so easy to learn quickly, but basically a fundamental hacking technique) and also standardizes the SQL syntax across platforms, so if you switch to PGSQL from MySQL, you don't have to debug your statements (if you weren't aware, certain SQL queries are different depending on the database). And there's more benefits too, those are just the main two.
|
4

You simply add a new array as element values for the $menu array.

$menu = array();
while($row = mysql_fetch_array($query)) {
    $menu[] = array(
        'id' => $row['id'],
        'parentid' => $row['parentid'],
        'title' => $row['title']
    );
}
var_dump($menu);

EDIT: How to traverse the array (basically this is PHP 101 so I suggest looking up PHP arrays)

foreach($menu as $index => $record){
    echo "ID: {$record['id']} ParentID: {$record['parentid']} Title: {$record['title']}";
}

4 Comments

Haha two seconds difference! If this was a shootout I'd be dead.
thanks for the speedy response. Only thing is that this puts the array inside an array. so now i have: Array ( [0] => Array ( [categoryid] => 160 [catparentid] => 93 [catname] => Cables ) [1] => Array ( [categoryid] => 136 [catparentid] => 98 [catname] => Holders ) [2] => Array ( etc.... Can i get it so it's just one array with the values? Cheers!
Well how exactly would you store multiple records with multiple fields inside an array? Of course you must use a multidimensional array for that. There is no excuse for not knowing how to use multidimensional arrays and its btw pretty easy - look at my edited post.
sorry, learn my first bit of php about 1.5 months ago. Still getting there with the basics. But thats heaps for your help. Super valuable! :)Now i can get back to my task :)

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.