PHP / MySQL Associative Multidimensional Array:

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
shortaug
Forum Newbie
Posts: 13
Joined: Wed May 13, 2009 6:11 pm

PHP / MySQL Associative Multidimensional Array:

Post by shortaug »

Hello.

I have data in a MySQL Table that adheres to the below: (note, no index, could add if necessary)

group~~~~~setting~~~~~value
1~~~~~~~~~Babble~~~~~Yeah, sure
1~~~~~~~~~Dabble~~~~~No, rather not
2~~~~~~~~~Crab~~~~~~~Maybe
3~~~~~~~~~Flab~~~~~~~Nah, not today

I want to query this and add it to an associative multidimensional array.

I have the following:

Code: Select all

 
$getem = array(array());
 
while ($row = mysql_fetch_assoc($result)) {
     $getem[][] = $row['group'];
     $getem[][] = $row['setting'];
     $getem[][] = $row['value'];    
}
 

Of course, this is not associative. I thought if I changed $getem = array(array()); to $getem = array( array('group', 'setting', 'value') );, then it would be associative, but this isn't working.

My ultimate goal is this:

I have a second table in which the 'group' of the above table is a FK to the index of the second table. The end result, once processed and matched using PHP, is to print out the following:

Group Name 1
~~~Babble~~~Year, sure
~~~Dabble~~~No, rather not
Group Name 2
~~~Crab~~~~Maybe
Group Name 3
~~~Flab~~~~Nah, not today

Am I on the right track? You don't need to write the code for me, I'm just looking for a little direction.

Thanks
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: PHP / MySQL Associative Multidimensional Array:

Post by requinix »

Ehhh... close.
1. The [][] syntax is invalid.
2. You can put a key in the []s.
3. If $getem is the array then $getem[group] is an array of (for example) settings => values.

Code: Select all

$getem = array(); // an empty array. array(array()) is not empty
while ($row = mysql_fetch_assoc($result)) {
     if (!isset($getem[$row["group"]])) $getem[$row["group"]] = array():
     $getem[$row["group"]][$row["setting"]] = $row["value"];
}
Don't let all the brackets confuse you.

PS: You should put a regular index on group and a unique index on (group, setting).
shortaug
Forum Newbie
Posts: 13
Joined: Wed May 13, 2009 6:11 pm

Re: PHP / MySQL Associative Multidimensional Array:

Post by shortaug »

Thank you Tas, with your code I was able to get it to work. At one time I had something close to what you gave me, but couldn't figure out how to access the group data, so I gave up thinking I was doing something wrong.

Here is what I have, just to test accessing everything:

foreach ($getem as $gkey => $gvalue) {
foreach ($gvalue as $tkey => $tvalue) {
echo $gkey . " | " . $tkey . " | " . $tvalue . "<br />";
}
}

This outputs what I want, so I know I can now access everything. Thanks again, I learned more about PHP Bracket Notation! :-)
Post Reply