0

给定的是一个数组数组:

$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 中解决该问题的奖励积分。去!

编辑:我解决它的方式

为了表明您实际上并没有做我的工作,这是我的方法(感觉太复杂了)。

[...]

4

2 回答 2

1

如果你重新排序你想要的结果id = 1,分组的工作方式会变得更加清晰:

['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' => 'b', 'property_b' => '2', 'property_c' => 'x,y']
['id' => '1', 'property_a' => 'c', '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' => '3', 'property_c' => 'x,y']
['id' => '1', 'property_a' => 'c', 'property_b' => '3', 'property_c' => 'x,y']

使用原始数组的简化版本,我可以得到上面的:

$set1 = 'a,b,c';
$set2 = '1,2,3';

$array1 = explode(",",$set1);
$array2 = explode(",",$set2);

foreach($array1 as $set1_member) {
    foreach($array2 as $set2_member) {
        $collection[] = array($set1_member, $set2_member);
    }
}

其中,当我用 回显数组时json_encode,返回:

[
["a","1"],
["a","2"],
["a","3"],
["b","1"],
["b","2"],
["b","3"],
["c","1"],
["c","2"],
["c","3"]
]
于 2013-09-20T11:18:31.133 回答
1

对于 PostgreSQL

SELECT id, 
  unnest(property_a) as property_a,
  property_b,
  property_c
FROM (
SELECT id, 
  property_a,
  unnest(property_b) as property_b,
  property_c
FROM
  sets) AS q;

小提琴

或者更传统地使用不同表中的属性中的数据(property_c 可以作为字符串存储在一行中,但这并不是重点)

SELECT a.id, 
  a.property_a,
  b.property_b,
  array_agg(c.property_c) as property_c
FROM  a 
  JOIN b ON a.id=b.id
  JOIN c ON b.id=c.id
GROUP BY a.id, a.property_a,b.property_b;

小提琴

于 2013-09-20T12:37:28.837 回答