如何通过用逗号分割多个数组值来替换数组项

时间:2013-09-20 10:29:28

标签: php 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'
  )  
);

并且该数组中的每个项目应该被逗号分隔的一些但不是所有值拆分:

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

编辑:我解决它的方式

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

[...]

2 个答案:

答案 0 :(得分: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"]
]

答案 1 :(得分: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;

Fiddle

或者更传统的不同表中的属性数据(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;

Fiddle