Input:
$input = [
'category' => [
'1' => [
'name' => 'c1',
'attribute' => [
'1' => [
'name' => 'a1',
'option' => [
'1' => [
'name' => 'o1'
],
'2' => [
'name' => 'o2'
]
]
],
'2' => [
'name' => 'a2',
'option' => [
'3' => [
'name' => 'o3'
],
'4' => [
'name' => 'o4'
]
]
]
]
],
'2' => [
'name' => 'c2',
'attribute' => [
'3' => [
'name' => 'a3',
'option' => [
'5' => [
'name' => 'o5'
],
'6' => [
'name' => 'o6'
]
]
],
'4' => [
'name' => 'a4',
'option' => [
'7' => [
'name' => 'o7'
],
'8' => [
'name' => 'o8'
]
]
]
]
]
]
];
Output:
$data = [
['category' => 1, 'categoryname' => 'c1', 'attribute' => 1, 'attributename' => 'a1', 'option' => 1, 'optionname' => 'o1'],
['category' => 1, 'categoryname' => 'c1', 'attribute' => 1, 'attributename' => 'a1', 'option' => 2, 'optionname' => 'o2'],
['category' => 1, 'categoryname' => 'c1', 'attribute' => 2, 'attributename' => 'a2', 'option' => 3, 'optionname' => 'o3'],
['category' => 1, 'categoryname' => 'c1', 'attribute' => 2, 'attributename' => 'a2', 'option' => 4, 'optionname' => 'o4'],
['category' => 2, 'categoryname' => 'c2', 'attribute' => 3, 'attributename' => 'a3', 'option' => 5, 'optionname' => 'o5'],
['category' => 2, 'categoryname' => 'c2', 'attribute' => 3, 'attributename' => 'a3', 'option' => 6, 'optionname' => 'o6'],
['category' => 2, 'categoryname' => 'c2', 'attribute' => 4, 'attributename' => 'a4', 'option' => 7, 'optionname' => 'o7'],
['category' => 2, 'categoryname' => 'c2', 'attribute' => 4, 'attributename' => 'a4', 'option' => 8, 'optionname' => 'o8'],
];
Method I used:
$final = [];
foreach ($input as $k => $d) {
foreach ($d as $ki => $i) {
foreach ($i['attribute'] as $ka => $a) {
foreach ($a['option'] as $ko => $o) {
array_push($final, ['category' => $ki, 'categoryname' => $i['name'], 'attribute' => $ka, 'attributename' => $a['name'], 'option' => $ko, 'optionname' => $o['name']]);
};
}
}
};
I want to convert the $input(INPUT) data into $data(OUTPUT). I mentioned my method which I used. Is it the best way we can use or is there any better option in terms of faster execution? What would be a better approach, or is it possible to just convert it into a one-liner?