Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 553
chriscct7 Avatar asked Jan 31 '26 07:01

chriscct7


1 Answers

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]
like image 95
Jeremy J Starcher Avatar answered Feb 01 '26 23:02

Jeremy J Starcher