I have started to understand the 2D array and multi-dimensional array, some reference books mentioned it has different ways to declare them. Which methods will be declare the array more easier and easy to understand?
method 1:
$pgmCode = array
(
"Item1"=>array("...","..."=>array("..."=>1)),
......
)
Or method2:
$A['ABC']['...'] = '3';
$A['...']['...']['...'] = '2';
Furthermore, if I will build a database and calling the data as array store, which method will be preferred to use?
Thanks.
$pgmCode = array(
"item1" = array(
"subItem1" => true,
"subItem2" => array("subSubItem1", "subSubItem2"),
),
"item2" = array(
"subItem1" => true,
"subItem2" => array("subSubItem1", "subSubItem2"),
),
);
Watchout with mixed use of array and string item in an array, if you will do a in_array() on a mixed variable array it will always return true if you don't use the strict mode of the function.
in your method 1
in_array("something", $pgmCode['Item1']); //return true
in_array("something", $pgmCode['Item1'], true); //return false
$pgmCode = array
(
"Item1"=>array("...","..."=>array("..."=>1)),
"Item1"=>array("...","..."=>array("..."=>1)),
......
);
makes it more understandable.