Page 1 of 1

Arrays

Posted: Fri Oct 06, 2006 2:24 pm
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

Posted: Fri Oct 06, 2006 2:39 pm
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.

Posted: Fri Oct 06, 2006 7:19 pm
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