Jump to content

Multidimensional Arrays


jamkelvl

Recommended Posts

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?  :wtf:

 

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!

Link to comment
https://forums.phpfreaks.com/topic/188262-multidimensional-arrays/
Share on other sites

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

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 :|

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;
}

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];

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.