Skip to content

Instantly share code, notes, and snippets.

@aaronsummers
Last active July 15, 2021 14:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aaronsummers/7fbb10b4431862cf83931f3c9f0a1d18 to your computer and use it in GitHub Desktop.
Save aaronsummers/7fbb10b4431862cf83931f3c9f0a1d18 to your computer and use it in GitHub Desktop.
Create an array of arrays, building a multidimentional array with an array
<?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