zackaryka
zackaryka

Reputation: 484

Multi-dimensional array vs. multiple arrays

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 ?

Upvotes: 14

Views: 1681

Answers (1)

gprathour
gprathour

Reputation: 15333

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.

Upvotes: 14

Related Questions