I'm trying to take data that is received from form input and, in PHP, change the way it is ordered for use in a template creation module/app. I need it to be in a multidimensional array for use.
How the array currently looks
$templateData = [
0 => 'h',
1 => 1,
2 => 2,
3 => 3,
4 => 'c-1-3',
5 => 3,
6 => 5,
7 => 'c-2-3',
8 => 3,
9 => 'c-3-3',
10 => 'f',
11 => 2
];
How the array should look
$templateData = [
0 =>[ //header row
0 => [1,2,3] //column
],
1 =>[ //content row(s)
0 => [3,5], //column
1 => [3],
2 => [0]
],
2 => [ //footer row
0 => [2] //column
]
]
The 'h' represents the start of the header row of the template and 'c-1-3' represents a the start of a row with 3 columns starting with the first column. The 'f' represents the start of the footer row. I'm just drawing a blank right now and I can't wrap my head around it.
Here is where I'm at right now, but it's still not working as intended:
$elements = $request->input('elements'); //laravel code that grabs array input
$row = 0;
$column = 0;
foreach($elements as $key => $element) {
//if value is a letter
if(preg_match('/^[a-zA-Z]/', $element)) {
//if the 3rd letter in the string is a 1
if(!isset($element[2]) || $element[2] == '1') { $row++; }
//cycle through values after current key
for($i=$key+1; $i < count($elements);$i++){
//until you hit another letter
if(preg_match('/^[a-zA-Z]/', $elements[$i])) { break; }
$column++;
$temp[$row][$column][] = $elements[$i];
}
$column = 0;
}
}
The $templateData and $elements variables are the same in the the above and below context.