Build an Uneven Nested Multidimentional Array in PHP

Build an uneven nested multidimensional array in PHP is very useful since you can retrieve the dataset mixed up by data from main table database and the affiliate tables, in which they are in primary and foreign key relationship. The beauty of it is you can retrieve it once for all and the data within the array maintain the same primary and foreign key relationship as they are in database. To append affiliate array into main array can use:

array_merge($mainArray, $affliateArray);

When declare the $affliateArray, it is better to give it a unique key, for instance 'appendix' below:

$affliateArray = array(array(array( "font_cd" => 'S'
, "font_color" => '000000'
)
)
);

$affliateArray['mainArrayDimension']['appendix'] = array( "font_cd" => $newCode
, "font_color" => $newColore
);

Sure, it would increase a dimension. However, it would clearly distinct the keys affiliate array from the keys in main array. Please note, it is preferred to use array_merge() than array_push() for easy maintain the main array's dimension.

The purpose of this rather complicated arrangement is not only one is able to preserve the primary and foreign key relationship in database, but also all child records in database, into a denormalized data structure in array.


http://www.java-samples.com/showtutorial.php?tutorialid=997

No comments:

Post a Comment

Labels