Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I need help with some PHP code. I am retrieving data from a MySQL database using left joins. Based on these records I am creating nested arrays which I want to be clean, e.g.:

array(
 [0] = array(
  [0] = array(
   [0] = array(
    [0] = array(
      etc...
    )
   )
  ),
 [1] = array(
  [0] = array(
   [0] = array(
    [0] = array(
     etc...
    )
   )
  )
 )
)

Now my idea is to create multiple arrays and then use the key to match them together, e.g.:

$array1 = array([0] => array(id = 0)); // value = 
// match
$array2 = array([0] => array(...));  // key = id = value of the $array1 with key 0

Is it a good practice ? Or should I keep the nested arrays ?

share|improve this question
5  
best practice will depend entirely with that you you will use the array(s) for. –  Dagon Nov 22 '11 at 3:14
2  
Really depends on what kind of application you are building. You might want to keep it nested if you want access to the whole tree through one loop for example. –  looneydoodle Nov 22 '11 at 3:17

1 Answer 1

up vote 13 down vote accepted

There is nothing like a good or bad practice in such cases. All depends on what kind of work you are taking from arrays. If you want to access/traverse all arrays in your code in single time then it will be good to have them as nested arrays but if you need to traverse only one or two out of all then I think you should make them individual arrays rather than traversing all arrays just to access one or two.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.