";未定义偏移”;,错误在哪里?(数组,php)


"Undefined offset", where is the mistake? (array, php)

我做了一个数组,里面有一些东西,它非常完美!现在,我将数组返回到我的视图(html)中,并希望在我的html中使用它。。但它给了我一个错误信息:

Undefined offset: 2 (View: /path/data.blade.php)

但我不明白为什么。。在我的控制器里一切都很好。。数组运行得很好,在我返回html时也没有错(至少我看不到一个)

$ipsWithBytesDates = [];
        foreach ($topTenIp as $val)
        {
            $byte_execResult = shell_exec("grep $val /path/domain.log | awk '{print $10}'");
            $add = array_sum(explode("'n", $byte_execResult));
            $add = $add / 1024 / 1024;
            $add = round($add, 2);
        $date_execResult = shell_exec("grep $val /path/domain.log |  awk '{print $4,$5}'");
        $date_array = explode("'n", $date_execResult);
        $date_array_pop = array_pop($date_array);
        $mylastelement = end($date_array);
        $ipsWithBytesDates[] = [
            'ip' => $val,
            'bytes' => $add,
            'dates' => $mylastelement,
        ];
    }
    uasort($ipsWithBytesDates, function($a, $b) {
        if ($a['bytes'] == $b['bytes'])
            return 0;
        elseif ($a['bytes'] < $b['bytes'])
            return 1;
        else
            return -1;
    });
    uasort($ipsWithBytesDates, function($a, $b) {
        if ($a['dates'] == $b['dates'])
            return 0;
        elseif ($a['dates'] < $b['dates'])
            return 1;
        else
            return -1;
    });

我返回这样的变量:

 return view('/domains/data', [
            'ipsWithBytesDates' => $ipsWithBytesDates,
        ]);

我的html看起来是这样的:

@foreach($ipsWithBytesDates as $item)
                        <tr>
                            <td>{{ $item['ip'] }}</td>
                            <td>{{ $item['bytes'] }}</td>
                            <td> {{ $item['dates'] }}</td>
                        </tr>
@endforeach

我真的找不到错误。。。我被卡住了,有人能看看我的代码,也许能和我一起发现错误吗?

return view('/domains/data', [ 'ipsWithBytesDates' => $ipsWithBytesDates, ]);

应该是

return view('/domains/data', [ 'ipsWithBytesDates' => $ipsWithBytesDates ]);

如果这不起作用,请在返回视图之前粘贴dd($ipsWithBytesDates),并检查传递给视图的数组的结构。

基于该结构,您可以获得数组的正确值(key=>values)。