Arrays

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
ibanez270dx
Forum Commoner
Posts: 74
Joined: Thu Jul 27, 2006 12:06 pm
Location: Everywhere, California

Arrays

Post by ibanez270dx »

Hi,
I'm new to arrays, and I'm having a little trouble trying to get something done... I've been working with the info on PHP.net. So, I have four variables from a row - air_id, air_name, air_type, and air_fleet. However, there are multiple rows each with its own unique air_id. What I want to do is store the information in an array so I just call something like $array['4'] to get the air_name, air_type, and air_fleet that corresponds with the air_id being 4. This is what I have right now, which obviously doesn't work:

Code: Select all

$array[$acid] .= array("air_id" => $acid, "air_name" => $acname, "air_fleet" => $acfleet, "air_type" => $actype);

and this to display it: 

".$array['1']."
can someone help me out?

Thanks!
- Jeff
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

do a var_dump() or print_r() of your array and see how it is built (dimensionalized). That will tell you how to use it.
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

Use an object:

Code: Select all

if (version_compare(PHP_VERSION, '5') >= 0) {
    class Air
    {
        public function Air()
        {
            $i = 0;
            foreach ($this as $property => $value) {
                $this->$property = func_get_arg($i++);
            }
        }
        var $id;
        var $name;
        var $fleet;
        var $type;
    }
} else {
    class Air
    {
        public function __construct()
        {
            $i = 0;
            foreach ($this as $property => $value) {
                $this->$property = func_get_arg($i++);
            }
        }
        public $id;
        public $name;
        public $fleet;
        public $type;
        public function __set($name, $value)
        {
            throw Exception('Not property of ' . __CLASS__ . ' class');
        }
    }
}

$array = array(new Air(1, 'Bob', 'SuperFleet', 4),
               new Air(2, 'Jane', 'SuperFleet', 5));
echo $array[0]->name; // Bob
Post Reply