Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 ?

like image 611
zackaryka Avatar asked Nov 22 '11 03:11

zackaryka


People also ask

Is a multidimensional array an array of arrays?

A multidimensional array is an array containing one or more arrays.

What are multi-dimensional arrays?

A multidimensional array in MATLAB® is an array with more than two dimensions. In a matrix, the two dimensions are represented by rows and columns. Each element is defined by two subscripts, the row index and the column index.

Is two-dimensional array and multidimensional array same?

Two – dimensional array is the simplest form of a multidimensional array.


1 Answers

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.

like image 98
gprathour Avatar answered Sep 21 '22 05:09

gprathour