duan1983 2015-12-11 06: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 07: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 单片机 TC277 PWM
  • ¥15 在更新角色衣服索引后,Sprite 并未正确显示更新的效果该如何去解决orz(标签-c#)
  • ¥15 VAE代码如何画混淆矩阵
  • ¥15 求遗传算法GAMS代码
  • ¥15 雄安新区高光谱数据集的下载网址打不开
  • ¥66 android运行时native和graphics内存详细信息获取
  • ¥15 rk3566 Android11 USB摄像头 微信
  • ¥15 torch框架下的强化学习DQN训练奖励值浮动过低,希望指导如何调整
  • ¥35 西门子博图v16安装密钥提示CryptAcquireContext MS_DEF_PROV Error of containger opening
  • ¥15 mes系统扫码追溯功能
手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部