2015-12-22 93 views
-1

我有一个多维数组这有点儿像这样从多维数组创建独特的数组?

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个键中,我有与id,headline,body,title_generictext_generic相同的阵列。他们有相同的标识和标题,其余都是空的。然后在下一个键中填入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 
) 

任一阵列,我不知道该如何解释这更好的...

+1

对不起,你能用简单的术语再次解释逻辑吗?你想通过某个定义返回一个“唯一”的数组吗?或者你想将所有这些数组合并成一个字段的值由某个“唯一性”标准选择的数组?不,没有内置的功能。 – deceze

+1

嗯,这取决于。如果逻辑是附加信息(例如,'title_generic','text_generic'等)不会导致冲突(多个不同的'title_generic' ...),你可以这样做。别的,你会如何解决冲突?例如哪一个标题是正确的? –

+0

听起来像你想将所有信息合并到一个数组中。你必须解释如何决定选择哪一条信息,然后是否有多个选项(如@ F.M所说的“冲突解决方案”)。 – deceze

回答

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

这可能会做你想做的事情(这有点不清楚)。

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

+0

这与'array_unique($ array,SORT_REGULAR);'我只需要用'array()'替换'[]',它工作,有点儿。我会为此工作。谢谢 –

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 
) 
+0

感谢您的帮助,但事实证明,我将不得不以完全不同的方式处理这个问题(我忽略了我的阵列中有不同ID的事实:S) –

+0

@dingo_d你需要进一步的帮助吗?或者你能解决它? –

+0

我设法解决我的问题,我的sql查询生成这个数组,所以我现在很好:)谢谢 –