从多维数组创建唯一的数组?

时间:2015-12-22 13:19:41

标签: php arrays sorting multidimensional-array

我有一个多维数组,看起来像这样

Array
(
    [0] => Array
        (
            [id] => 140309
            [headline] => Random title
            [body] => 
            [title_generic] => 
            [text_generic] => 
        )

    [1] => Array
        (
            [id] => 140309
            [headline] => Random title
            [body] => 
            [title_generic] => 
            [text_generic] => 
        )

    [2] => Array
        (
            [id] => 140309
            [headline] => Random title
            [body] => 
            [title_generic] => 
            [text_generic] => 
        )

    [3] => Array
        (
            [id] => 140309
            [headline] => Random title
            [body] => 
            [title_generic] => 
            [text_generic] => 
        )

    [4] => Array
        (
            [id] => 140309
            [headline] => Random title
            [body] => 
            [title_generic] => Random title
            [text_generic] => [b]This is Random title:[/b] 16 nov  2012
        )

    [5] => Array
        (
            [id] => 140309
            [headline] => Random title
            [body] => 
Some text goes here. Blaaaa
            [title_generic] => 
            [text_generic] => 
        )

    [6] => Array
        (
            [id] => 140309
            [headline] => Random title
            [body] => 
            [title_generic] => 
            [text_generic] => 
        )

)

我尝试使用array_unique()过滤它,但只返回

Array
    (
        [0] => Array
            (
                [id] => 140309
                [headline] => Random title
                [body] => 
                [title_generic] => 
                [text_generic] => 
            )
    )

但我想要

Array
    (
        [0] => Array
            (
                [id] => 140309
                [headline] => Random title
                [body] => 
                Some text goes here. Blaaaa
                [title_generic] => Random title
                [text_generic] => [b]This is Random title:[/b] 16 nov  2012
            )
    )

那就是只返回全部填充的唯一字段。

在数组中只有一个唯一的填充空格,所以在第一个键中我没有办法title_generic然后我会在第三个左右不同。 bodytext_generic也是如此。它们在某些数组中只出现一次。但是id,标题等都是一样的(里面有日期等等)。

有没有能做这种事情的功能?

修改

我可能不太清楚。我想返回包含来自其他键(来自该键中的数组的值)的所有信息的数组,这些数据是不同的。因此,在数组的前4个键中,我使用idheadlinebodytitle_generictext_generic具有相同的数组。他们有相同的ID和标题,其余的都是空的。然后在下一个键中填充title_generictext_generic,依此类推。

我需要一个具有填充键的数组,如

Array
    (
        [0] => Array
            (
                [id] => 140309
                [headline] => Random title
                [body] => 
                Some text goes here. Blaaaa
                [title_generic] => Random title
                [text_generic] => [b]This is Random title:[/b] 16 nov  2012
            )
    )

Array
(
    [id] => 140309
    [headline] => Random title
    [body] => 
    Some text goes here. Blaaaa
    [title_generic] => Random title
    [text_generic] => [b]This is Random title:[/b] 16 nov  2012
)

我不知道如何更好地解释这个......

2 个答案:

答案 0 :(得分:2)

$result = array_reduce($array, function (array $result, array $item) {
    return array_filter($result) + $item;
}, []);

这可能会做你想要的(稍微不清楚)。

说明:它逐个浏览你的每个项目;它会过滤掉所有空值,只留下填充的键(array_filter);然后,它会将所有不存在的密钥(+从下一个项目添加到它(在array_reduce上阅读)。最终结果应该是一个数组,其中所有数组中的所有非空键合并为一个,其值是循环中遇到的第一个非空值。

答案 1 :(得分:1)

我知道已有解决方案。尽管如此,我想在执行时间和相同功能方面为您提供一种轻量级方法。

# your data here
$array = [
  [
      'id' => '123',
      'headline' => 'one two three',
      'body' => 'somebody',
      'title_generic' => '',
      'text_generic' => '',
  ],
  [
      'id' => '123',
      'headline' => 'one two three',
      'body' => null,
      'title_generic' => 'title',
      'text_generic' => 'text',
  ],
];

# the aggregate to be created
$aggregate = [];

foreach ($array as $el) {
    if (empty($el)) continue;

    foreach ($el as $k => $v) {
        if (empty($v)) continue;

        if (!isset($aggregate[$k])) {
            $aggregate[$k] = $v;
        }
    }
}

# debug print
echo '<pre>';print_r($aggregate);echo '<pre>';

# the output
Array
(
    [id] => 123
    [headline] => one two three
    [body] => somebody
    [title_generic] => title
    [text_generic] => text
)