Jump to content

mysql to multidimensional array.


jordz

Recommended Posts

Hi All,

Im recently new to the forum and need a little help with this array problem ive been stuck on for a few hours. I'm a newby really and trying to learn more and more. Ive tried using the foreach function but so far had no luck, as well as looking on php.net.

Basically I have some code that has multiple arrays in it however I can't seem to get it from this (which is how it is now):

code:

require_once('config.php');
echo '<pre>';
$select = 'SELECT *  FROM photos';
$runquery1 = mysql_query($select);
while($photos = mysql_fetch_array($runquery1, MYSQL_ASSOC))

print_r($photos);

Array

(

    [photo_id] => 2

    [user_id] => 2

 

)

Array

(

    [photo_id] => 3

    [user_id] => 2

)

 

 

 

to something along the lines of this:

[0] => Array
     (
        [photo_id] => 2
        [user_id] => 2
   
      )
[1] => ]Array
   (
    [photo_id] => 3
    [user_id] => 2
   )

 

basically I want to be able to access each arrays array like so

echo $photos['1']['user_id]; etc...

 

any help would be much appreciated,

 

thanks ever so much,

 

Jordan

Link to comment
https://forums.phpfreaks.com/topic/193597-mysql-to-multidimensional-array/
Share on other sites

ok so all you need to do is to load each extracted array into a master array.

require_once('config.php');
$allphotos = array();
$select = 'SELECT *  FROM photos';
$runquery1 = mysql_query($select);
while($photos = mysql_fetch_array($runquery1, MYSQL_ASSOC))
{
       $allphotos[] = $photos;
}

 

Then you can address $allphotos like $allphotos[0]['user_id']  etc

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.