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'];
}
share|improve this question

2 Answers

up vote 2 down vote accepted

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.

share|improve this answer
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! – jason3w Jul 13 '12 at 0:27
1  
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 :) – Steve Jul 13 '12 at 0:28
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! – jason3w Jul 13 '12 at 0:38
No worries, good luck. And have a look at PDO - did I mention it's easy? :) – Steve Jul 13 '12 at 0:55
1  
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. – Steve Jul 13 '12 at 2:12
show 1 more comment

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: {$values['id']} ParentID: {$record['parentid']} Title: {$record['title']}";
}
share|improve this answer
1  
Haha two seconds difference! If this was a shootout I'd be dead. – Steve Jul 13 '12 at 0:14
Missed it. Added. Thanks. – holodoc Jul 13 '12 at 0:17
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! – jason3w Jul 13 '12 at 0:22
1  
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. – holodoc Jul 13 '12 at 0:30
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 :) – jason3w Jul 13 '12 at 0:33

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.