jamkelvl Posted January 12, 2010 Share Posted January 12, 2010 I know it's possible to create multidimensional arrays but I'm wondering if this is possible or how to do this... My program currenty connects a database and grabs all rows for the selected table. $id, $location_name, $requested_by, $address, $phone_number_1, $phone_number2, $e_mail What I would like to happen is when iterating thru the database I want to create a matrix array like... while($row = mysql_fetch_array($result)) { extract($row); $locations[$id] = $location_name => array($requested_by, $address, $phone_number_1, $phone_number2, $e_mail); } Understand? I believe this may be a little more accurate... while($row = mysql_fetch_array($result)) { extract($row); $locations = array($id => array($location_name, $requested_by, $address, $phone_number_1, $phone_number2, $e_mail)); } ...but in theory I believe the loop should be inside the $locations array if at all possible? Like... $locations = array( while($row = mysql_fetch_array($result)) { extract($row); $id => array($location_name, $requested_by, $address, $phone_number_1, $phone_number2, $e_mail) } ); AHHHH! Any help would be greatly appreciated! Quote Link to comment Share on other sites More sharing options...
mikesta707 Posted January 12, 2010 Share Posted January 12, 2010 The third example is completely wrong, and doesn't make any sense really. you probably want to do something like $locations = array();//create an empty array while(blah blah){ extract($row); $locations[] = array($location_name, $requested_by, $address, $phone_number_1, $phone_number2, $e_mail);//push stuff into array } print_r($locations);//show structure of the array However, the keys won't be preserved. The new array will become numeric (which may or may not be what you want). You could preserve the keys by simply doing $location[] = $row; in the while loop Quote Link to comment Share on other sites More sharing options...
jamkelvl Posted January 12, 2010 Author Share Posted January 12, 2010 Hmm after seeing your post I thought you had the right idea but I think I may have explained it wrong, haha. Okay, I want it to be something like this.. $locations = array(); //blah blah blah i am a loop $locations[$id] = $details[0] = $location_name; $details[1] = $requested_by; $details[2] = $address; $details[3] = $phone_number_1; $details[4] = $phone_number_2; $details[5] = $e_mail; //I am the end of the loop hurray! echo $locations[1][$details[0]]; Lol, something like that :| Quote Link to comment Share on other sites More sharing options...
Psycho Posted January 12, 2010 Share Posted January 12, 2010 That makes no sense, you would be redefining the variables ($details[1], $details[2], etc.) on each iteration. I *think* this may be what you are after: //Create the array variable $locations = array(); //Iterate through the result set while($row = mysql_fetch_array($result)) { //Add to the multidim array using the record id as the index $locations[$row['id']] = $row; } Quote Link to comment Share on other sites More sharing options...
jamkelvl Posted January 12, 2010 Author Share Posted January 12, 2010 Hmm.. Not really sure what that does? can I display it like...and also shouldn't the code look something like this (I'm not sure) <?php //Create the array variable $locations = array(); //Iterate through the result set while($row = mysql_fetch_array($result)){ //Add to the multidim array using the record id as the index $locations[$row[$id]] = $row; } // 1 is = $id echo $locations['1'][0]; echo $locations['1'][1]; echo $locations['1'][2]; echo $locations['1'][3]; echo $locations['1'][4]; echo $locations['1'][5]; Quote Link to comment Share on other sites More sharing options...
jamkelvl Posted January 12, 2010 Author Share Posted January 12, 2010 Okay, after playing around with it and realized I was trying to debug -the wrong way- and well yeah your code works exactly as I want it to Thank-you VERY much sir, brilliant! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.