I have an input array like this
$input = [
["relation" => "OR"],
["color" => 'green'],
["color" => 'yellow'],
["relation" => "AND"],
["color" => 'black'],
["color" => 'orange'],
["relation" => "OR"],
["color" => 'blue'],
["color" => 'violet'],
];
Desired result:
$output = array(
'relation' => 'OR',
array(
"color" => 'green'
),
array(
"color" => 'yellow'
),
array(
"relation" => "AND",
array(
"color" => "black"
),
array(
"color" => "orange"
),
array(
"relation" => "OR",
array(
"color" => "blue"
),
array(
"color" => "violet"
),
)
)
);
I believe I need to do make recursive function to handle this.
I have this but only works for a single level
function generate_nested_array($array, $nested_tax_query = array(), $target_index = 0)
{
//the first element is always the relation
$nested_tax_query['relation'] = $array[0]['relation'];
// unset the first element as it is not needed anymore
unset($array[0]);
$len = count($array);
// reindex the array
$array = array_values($array);
foreach ($array as $element_key => $element) {
if (isset($element['relation'])) {
$target_index = $element_key;
break;
}
}
// put everything below the target index into the target index and leave the rest as it is
for ($i = 0; $i < $len - 1; $i++) {
if ($i < $target_index) {
$nested_tax_query[] = $array[$i];
} else {
$nested_tax_query[$target_index][] = $array[$i];
}
}
// last item in the nested array
$len_nested = count($nested_tax_query);
// last item in the in the nested array
$last_item_in_nested_array = $nested_tax_query[$len_nested - 2];
return $nested_tax_query;
}
Is my approach correct?