Last active
July 15, 2021 14:18
-
-
Save aaronsummers/7fbb10b4431862cf83931f3c9f0a1d18 to your computer and use it in GitHub Desktop.
Create an array of arrays, building a multidimentional array with an array
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
The important thing here is having 2x empty arrays. | |
The first array is the parent - level 1 array | |
The second array is the child - level 2 array | |
*/ | |
$examples = array( | |
'item 1', | |
'item 2', | |
'item 3', | |
'item 4', | |
'item 5', | |
); | |
$total_child_el = 3; | |
$parent_array = array(); | |
foreach ( $examples as $example ) { | |
$child_array = array(); | |
for ($i=0, $i < $total_child_el, $i++) { | |
$child_array[] = $example; | |
} | |
$parent_array[] = $child_array; | |
} | |
// OUTPUT | |
array( | |
array('item 1','item 1','item 1'), | |
array('item 2','item 2','item 2'), | |
array('item 3','item 3','item 3'), | |
array('item 4','item 4','item 4'), | |
array('item 5','item 5','item 5') | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment