给定的是一个数组数组:
$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'
)
);
并且此数组中的每个项目都应该由一些但不是所有用逗号分隔的值分割:
$splitItemsBy = array('property_a', 'property_b');
结果应该是具有定义的 $splitItemsBy 键值的唯一组合的项目数组。
期望的结果:
[
['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']
]
在 PHP 中是否有一种高效且优雅的方式来执行此操作?
预先感谢您的所有提交。在 SQL 中解决该问题的奖励积分。去!
编辑:我解决它的方式
为了表明您实际上并没有做我的工作,这是我的方法(感觉太复杂了)。
[...]