我有一个数组,其中包含如下数据:
Array (
[google.com] => Array
(
[youseo.com] => 1,
[seo.com] => 1,
[linkbuilding.com] => 0,
[seo556.com] => 1,
)
[youtube.com] => Array
(
[youseo.com] => 0,
[seo.com] => 0,
[linkbuilding.com] => 0,
[seo556.com] => 1,
)
[facebook.com] => Array
(
[youseo.com] => 0,
[seo.com] => 1,
[linkbuilding.com] => 1,
[seo556.com] => 0,
)
)
我需要填充url的新数组和计数值:
Array
(
[google.com] => 3
[youtube.com] => 1
[facebook.com] => 2
)
请为此提供帮助吗?
答案 0 :(得分:2)
您可以使用array_values
和array_sum
:
$counts = array();
foreach ($array as $page_name => $record)
$counts[$page_name] = array_sum(array_values($record));
但是假设这些值只能是0或1。
答案 1 :(得分:1)
如果您的数组是$a
:
$counts = array();
foreach ($a as $page_name => $page)
{
foreach ($page as $name => $value)
{
$counts[$page_name] += $value;
}
}
答案 2 :(得分:0)
只需将array_map
与array_sum
$result = array_map(function($el) {
return array_sum($el);
}, $data);