duan1983 2015-12-11 14:27
浏览 80
已采纳

“未定义的偏移量”,哪里出错? (数组,php)

I did a array with some things in it and it works just perfect! now i returned the array to my view (the html) and want to use it in my html.. but it gives me this error message:

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

but I can't see why.. in my controller it's all fine.. the array works perfectly and in my returning to the html is no mistake too (at least I can't see one)

$ipsWithBytesDates = [];
        foreach ($topTenIp as $val)
        {
            $byte_execResult = shell_exec("grep $val /path/domain.log | awk '{print $10}'");
            $add = array_sum(explode("
", $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("
", $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;
    });

I'm returning the variable like this:

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

and my html looks like this:

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

I really can't find the mistake... I'm stuck, could anybody look over my code and maybe find with me the mistake?

  • 写回答

1条回答 默认 最新

  • douying1119 2015-12-11 15:20
    关注

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

    Should be

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

    If this won't work, please paste dd($ipsWithBytesDates) before the return view and check what the structure is of your array you are passing to the view.

    Based on that structure you could get the right (key => values) of the array.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 如何绘制动力学系统的相图