chriscct7
chriscct7

Reputation: 527

Jagged Edge Arrays in PHP

I want to store some data in (I guess a semi-2, semi-3d array) in PHP (5.3) What I need to do is store data about each floor like this:

Floor    Num of Spots    Handicap          Motorcyle        Other
1         100            array(15,16,17)    array (47,62)   array (99,100)
2         100            array(15,16,17)    array (47,62)   array (99,100)
and on

The problem is, is if the Handicap+Motorcyle+Other were ints, I could just store the data in a 2d array. However, they aren't. So I was thinking I could make something almost like a 3D array, with the first two columns only being in 2D.

The other thought I had was making a 2D array and for columns 3,4, and 5 instead of saving as

array(15,16)
//save like
1516

And then split at two digits (1 digit array numbers would be prefaced with a 0). However, I am wondering about the limit of the length of a string, because if I decide to move to a 3 digit length number in the array, like array(100, 104), and I need to store alot of numbers, I am thinking I am going to quickly exceed the max.

Edit 1 I like Omar's answer alot, but I'm not sure as to how to pull the data out.

Upvotes: 2

Views: 767

Answers (4)

Basic
Basic

Reputation: 26766

Something like...

$floors = array();
$floors[1] => array(
    Spaces => 100,
    Handicapped => array(15, 16, 17),
    Motorcycle => array(47, 62),
    Other => array (1, 2, 3, ..., n)
)

You can then retrieve the values as ...

$Floor1Spaces = $floors[1]['Spaces']; //An integer
$Floor1HAndicapped = $floors[1]['Handicapped']; //A 1-dimensional array of integer

Upvotes: 1

Jeremy J Starcher
Jeremy J Starcher

Reputation: 23863

While you could store them as ?D array, there is another approach you might want to consider :

$stuff = array (
  'floor1' => 
  array (
    'NumSpots' => 100,
    'handicap' => array (15,16,17),    
    'motorcycle' => array (47, 62),
    'other' =>  array (99, 100),
    ),
  ),
  'floor2' => 
    'NumSpots' => 100,
    'handicap' => array (15,16,17),    
    'motorcycle' => array (47, 62),
    'other' => array (99, 100),
    ),
  )
)

That way, you can access things through mroe meaningful names like

$stuff['floor1']['motorcycle'][2]

Upvotes: 5

sachleen
sachleen

Reputation: 31141

You can do exactly that. Example:

$a = array();
$a[1] = array(
    'spots' => 100,
    'handicap' => array(5,3,5)
);
$a[2] = array(
    'spots' => 50,
    'handicap' => array(1,3,20)
);
var_dump($a);

Output:

array
  1 => 
    array
      'spots' => int 100
      'handicap' => 
        array
          0 => int 5
          1 => int 3
          2 => int 5
  2 => 
    array
      'spots' => int 50
      'handicap' => 
        array
          0 => int 1
          1 => int 3
          2 => int 20

You can use the array indices for the floor number or have a separate key/value for that.

Upvotes: 1

DiverseAndRemote.com
DiverseAndRemote.com

Reputation: 19888

In php you can have named keys for your arrays. Each element in your array can have different types so you could have

$floors = array(
    1 => array(
            'num_spots' => 100,
            'handicap' => array(15,16,17)
        ),   

    2 => array(
            'num_spots' => 100,
            'handicap' => array(15,16,17),
            'motorcycle' => array (47,62)
        )
);

etc...

Upvotes: 2

Related Questions