Given is an array of arrays:
$items = array(
array(
'id' => '1',
'property_a' => 'a,b,c',
'property_b' => '1,2,3'
'property_c' => 'x,y'
),
array(
'id' => '2',
'property_a' => 'b,c,d',
'property_b' => '3,4,5',
'property_c' => 'x,y'
)
);
and each item in this array should be split by some but not all values that are seperated by comma:
$splitItemsBy = array('property_a', 'property_b');
The result should be an array of items with unique combinations of the defined $splitItemsBy key values.
Desired result:
[
['id' => '1', 'property_a' => 'a', 'property_b' => '1', 'property_c' => 'x,y']
['id' => '1', 'property_a' => 'b', 'property_b' => '1', 'property_c' => 'x,y']
['id' => '1', 'property_a' => 'c', 'property_b' => '1', 'property_c' => 'x,y']
['id' => '1', 'property_a' => 'a', 'property_b' => '2', 'property_c' => 'x,y']
['id' => '1', 'property_a' => 'a', 'property_b' => '3', 'property_c' => 'x,y']
['id' => '1', 'property_a' => 'b', 'property_b' => '2', 'property_c' => 'x,y']
['id' => '1', 'property_a' => 'b', 'property_b' => '3', 'property_c' => 'x,y']
['id' => '1', 'property_a' => 'c', 'property_b' => '2', 'property_c' => 'x,y']
['id' => '1', 'property_a' => 'c', 'property_b' => '3', 'property_c' => 'x,y']
['id' => '2', 'property_a' => 'b', 'property_b' => '3', 'property_c' => 'x,y']
['id' => '2', 'property_a' => 'c', 'property_b' => '3', 'property_c' => 'x,y']
['id' => '2', 'property_a' => 'd', 'property_b' => '3', 'property_c' => 'x,y']
['id' => '2', 'property_a' => 'b', 'property_b' => '4', 'property_c' => 'x,y']
['id' => '2', 'property_a' => 'b', 'property_b' => '5', 'property_c' => 'x,y']
['id' => '2', 'property_a' => 'c', 'property_b' => '4', 'property_c' => 'x,y']
['id' => '2', 'property_a' => 'c', 'property_b' => '5', 'property_c' => 'x,y']
['id' => '2', 'property_a' => 'd', 'property_b' => '4', 'property_c' => 'x,y']
['id' => '2', 'property_a' => 'd', 'property_b' => '5', 'property_c' => 'x,y']
]
Is there an performant and elegant way to do this in PHP?
Thank you in advance for all submissions. Bonus Points for solving that in SQL. Go!
Edit: The way I solved it
To show that you don't actually do my work, here's my approach (which kind of feels too complicated).
[...]