如何在foreach循环中计算两个数组

时间:2018-12-14 06:24:37

标签: php laravel loops foreach laravel-blade

大家好,我有2条路径,所以我返回2个任务数组,我需要对每个数组进行计数,所以最后我得到了

[6,4]

@foreach ($path->pathtags as $Tag)    
    @foreach ($Tag->Tasks as $Task) 
        @if (!in_array($Task->id,$a))
            <li class="list-group-item"> Task : {{ $Task->task_name }}  </li>
        @endif
    @endforeach
@endforeach

2 个答案:

答案 0 :(得分:0)

您应该尝试以下操作:

$path = count($path->pathtags);
$task = count($Tag->Tasks);

更新后的答案

$path = count($path->pathtags);
@foreach ($path->pathtags as $Tag)
    $task = count($Tag->Tasks);
    @foreach ($Tag->Tasks as $Task) 
        <li class="list-group-item"> Task : {{ $Task->task_name }}  </li>
    @endforeach
@endforeach

答案 1 :(得分:0)

最简单的php数组计数方法是count($array),因此,如果要使用2个数组计数创建一个新数组,可以这样:

$counts = [count($array1), count($array2)];

但是,如果您需要计算数据库中的记录,则需要更改查询而不是->get(),而需要使用->count()