[英]Create unique array from 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
,然后在第三個左右使用不同的鍵。 body
和text_generic
。 它們在某些數組中只出現一次。 但是 id、標題等總是相同的(里面有一個日期等等)。
有沒有一個函數可以做這樣的事情?
編輯
大概是我說的不夠清楚。 我想返回包含來自其他鍵(該鍵中數組的值)的所有信息的數組,這些信息是不同的。 所以在陣列中的前4個凱斯我有相同的陣列id
, headline
, body
, title_generic
和text_generic
。 它們具有相同的 id 和標題,其余為空。 然后在下一個鍵中填充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
)
)
或者
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
)
我不知道如何更好地解釋這一點......
$result = array_reduce($array, function (array $result, array $item) {
return array_filter($result) + $item;
}, []);
這可能會做你想要的(這有點不清楚)。
說明:它一件一件地遍歷你的每件物品; 它過濾掉所有空值,只留下填充的鍵( array_filter
); 然后它將所有不存在的鍵( +
)從下一項添加到它(在array_reduce
上array_reduce
)。 最終結果應該是一個數組,其中所有數組中的所有非空鍵合並為一個,該值是循環中遇到的第一個非空值。
我知道已經有解決方案了。 盡管如此,我想向您展示一種在執行時間和功能相同的方面的輕量級方法。
# 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
)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.