例如:
Array
(
[0] => Array
(
[id] => 1
[name] => Test
[slug] => test
[services] => Array
(
[0] => Array
(
[name] => test
)
[1] => Array
(
[name] => test
)
[2] => Array
(
[name] => test
)
[3] => Array
(
[name] => test
)
)
)
[1] => Array
(
[id] => 1
[name] => Test
[slug] => test
[services] => Array
(
[0] => Array
(
[name] => test
)
[1] => Array
(
[name] => test
)
[2] => Array
(
[name] => test
)
[3] => Array
(
[name] => test
)
)
)
这只是虚拟数据,但是我想计算每个数组中的服务数量,但我正在寻找一种有效的方法。
现在我要这样做的方式是:
foreach($arrayAbove as $array){
$i = 0;
foreach($array['services'] as $array){
$i++;
}
}
有更好的方法吗?
答案 0 :(得分:4)
public void method(int a)
{
if (imgLine0 != null)
{
switch (a)
{
case 0:
imgLine0.Visibility = Visibility.Visible;
imgLine1.Visibility = Visibility.Collapsed;
imgLine2.Visibility = Visibility.Collapsed;
break;
case 1:
imgLine0.Visibility = Visibility.Collapsed;
imgLine1.Visibility = Visibility.Visible;
imgLine2.Visibility = Visibility.Collapsed;
break;
case 2:
imgLine0.Visibility = Visibility.Collapsed;
imgLine1.Visibility = Visibility.Collapsed;
imgLine2.Visibility = Visibility.Visible;
break;
}
}
}
如果你想要整体计算:
foreach($arrayAbove as $array){
$i = count($array['services']);
print_r($i);
}
答案 1 :(得分:1)
这是一种有趣的方式:
$result = array_reduce($array, function($c, $v) { return $c + count($v['services']); });
答案 2 :(得分:0)
foreach($arrayAbove as $array){
$sub_array = sizeof($array['services']);
$total = $total + $sub_array;
}
echo 'Sub Array count is : '.$total;