I need an advice, how to create tree view from folowing source.
$dataSrc = array(
'1@02@43@170@1',
'1@02@43@176@1',
'1@02@43@182@1',
'1@02@42@182@1',
'1@02@42@176@1',
);
This strings are grouped variants of product. Each parameter is separated by @. So if I explode this strings I will get theese.
Array
(
[0] => Array
(
[0] => 1
[1] => 02
[2] => 43
[3] => 170
[4] => 1
)
[1] => Array
(
[0] => 1
[1] => 02
[2] => 43
[3] => 176
[4] => 1
)
[2] => Array
(
[0] => 1
[1] => 02
[2] => 43
[3] => 182
[4] => 1
)
[3] => Array
(
[0] => 1
[1] => 02
[2] => 42
[3] => 182
[4] => 1
)
[4] => Array
(
[0] => 1
[1] => 02
[2] => 42
[3] => 176
[4] => 1
)
)
Now I need to get combinations for parameters, which is not same. Result should be:
$resp = array(
43 => array(
170,
176,
182,
),
42 => array(
182,
176,
)
);
So unset keys, which are same and get tree from cobninations. Can you please help me with that?
Additional details (edited into question from OP's comments -- some of which were deleted):
These strings (1@02@43@170@1) are combined variants of products (returned from external DB) 1 - some param 02 - color 43 - sizeA 178 - sizeB Each product is available in specific combinations, which are declared by these strings. So if I will have 1@02@43@170@1 and 1@02@43@176@1 it means that i have product with size 43 and in size 43 I have another variants 176 and 170. It's really hard to explain, but i need these values grouped.
I do not know which values are different. First of all, I need to know what parameters need to be grouped (it can be for example two or three params).
if values are same, it should not be grouped. If there will be 3 different values, it should be like that: $final = array( '02' => array( 42 => array( 170, 176, ), 43 => array( 182, 176, ), ), '03' => array( 42 => array( 170, 176, ), ) );
It should be grouped hierarchical, so first different value at parent level.
Another possible sample set:
$dataSrc2 = array(
'1@02@43@170@1',
'1@02@43@176@1',
'1@02@43@182@1',
'1@02@42@182@1',
'1@02@42@176@1',
'1@03@43@170@1',
'1@03@43@176@1',
'1@03@43@182@1',
'1@03@42@182@1',
'1@03@42@176@1',
);
After parsing the array of @
-delimited strings into a nested structure with a known depth, you are seeking a lean output array of unknown depth whereby the top level has more than one entry.
To accomplish this, my code below will parse the strings into the limited-depth array structure, then recursively strip away the top level(s) if there is only one entry. The resulting array will either be an empty array (if all strings are identical) or it will have a top level with multiple entries.
Code: (Demo with 3 sample sets)
function killYourParentIfYouHaveOne($level) {
if (count($level) === 1) {
$key = key($level);
$level = is_array($level[$key]) ? killYourParentIfYouHaveOne($level[$key]) : [];
}
return $level;
}
function differingTree(array $array): array {
$result = [];
foreach ($array as $item) {
[$a, $b, $c, $d, $e] = explode('@', $item, 5);
$result[$a][$b][$c][$d] = $e;
}
return killYourParentIfYouHaveOne($result);
}
foreach ($tests as $test) {
echo var_export(differingTree($test), true) . "\n---\n";
}
Sample Inputs:
$tests = [
[
'1@02@43@170@1',
'1@02@43@176@1',
'1@02@43@182@1',
'1@02@42@182@1',
'1@02@42@176@1',
],
[
'1@02@43@170@1',
'1@02@43@176@1',
'1@02@43@182@1',
'1@02@42@182@1',
'1@02@42@176@1',
'1@03@43@170@1',
'1@03@43@176@1',
'1@03@43@182@1',
'1@03@42@182@1',
'1@03@42@176@1',
],
[
'1@02@43@170@1',
'1@02@43@170@1',
]
];
Generated Outputs:
array (
43 =>
array (
170 => '1',
176 => '1',
182 => '1',
),
42 =>
array (
182 => '1',
176 => '1',
),
)
---
array (
'02' =>
array (
43 =>
array (
170 => '1',
176 => '1',
182 => '1',
),
42 =>
array (
182 => '1',
176 => '1',
),
),
'03' =>
array (
43 =>
array (
170 => '1',
176 => '1',
182 => '1',
),
42 =>
array (
182 => '1',
176 => '1',
),
),
)
---
array (
)
---