Question

I'm working on a PHP script to hold a lot of information. Lets try to explain our situation!

I have actually 33 different stations. For each of that 33 stations I have 5 different categories. And for each of that 33 stations with each 5 different categories i have 37 different values per category.

Do I need an 2d of 3d array for store this information in it ?

Thanks you!

Était-ce utile?

La solution

Something like this will work, just add more data as needed:

$station_array =  
    array(
        'station1' => array(
            'cat1' => array ('val1','val2','val3'),
            'cat2' => array ('val1','val2','val3'),
            'cat3' => array ('val1','val2','val3')
        ),
        'station2' => array (
            'cat1' => array ('val1','val2','val3'),
            'cat2' => array ('val1','val2','val3'),
            'cat3' => array ('val1','val2','val3')
        ),
        'station3' => array (
            'cat1' => array ('val1','val2','val3'),
            'cat2' => array ('val1','val2','val3'),
            'cat3' => array ('val1','val2','val3')
        )
    );

Autres conseils

Sounds like a job for a relational database!

But you're correct in your initial assumption. You will need a 3-dimensional array to hold your information because your data has 3 tiers: the stations, the categories, and the values.

A php array will be fine for this

$MyArray = array('Station1' => array('Category1' => 
                                      array('Value1'=> 1000,'Value2'=> 1001), 
                                     'Category2' => array('Value1' => 2332)), etc...
                 'Station2' => array('Category1' => 
                                      array('Value1'=> 1000,'Value2'=> 1001), 
                                     'Category2' => array('Value1' => 2332)), etc
                 etc

 );

Once you pass more than two dimensions in an associative array, it's good to start considering using objects to store your information. Objects make it a lot easier to understand how things are organized, they can enforce validation restrictions on your data (make sure it's in the form it should be), and you can call functions on the data to manipulate it (instead of having random external functions manipulating your entire array). An example would be:

class CategoryValue {
    var $val; // your value

    function __construct($val) {
        $this->val = $val;
    }
}

class Category {
    var $values = array(); // an array of CategoryValue objects

    function addValue(CategoryValue $val) {
        $this->values[] = $val;
    }
}

class Station {
    var $categories = array(); // an array of Category objects

    function addCategory(Category $category) {
        $this->categories[] = $category;
    }
}

well, all depends on how you want to acheive, if you dont need to loop through the values, but you just want to store data and alway know whay you want to get, you could use hashes for that, the table would look like:

$data = array(
   md5('StationX'.'CategoryY'.'PropertyZ') => 'ValueU',
   md5('StationA'.'CategoryB'.'PropertyC') => 'ValueQ'
);

This way you can get the data right away and dont have to bother to check if you initialised CategoryB array for StationA when you want to add value for PropertyZ

  1. php stores associative arrays as hastables technically
  2. ... thats all if you really insist on not using databases ;)
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top