Php 将数组返回到视图不起作用

Php 将数组返回到视图不起作用,php,arrays,laravel,Php,Arrays,Laravel,我有一个数组,里面有一些排序的数字。数组就像我想要的一样,所以我将它返回到视图中。现在我想在我的视图(html)中得到数组的输出,php说:htmlentities()期望参数1是字符串,数组给定 这是我的第一个问题。。第二个问题是,我希望数组中的输出相互关联 比如: number number number number ..... 但它类似于:输出中的number 我真的需要这个为我的项目,我有我需要的一切。。只是输出的格式不好 以下是我在控制器中的代码: $mb_bytes = arr

我有一个数组,里面有一些排序的数字。数组就像我想要的一样,所以我将它返回到视图中。现在我想在我的视图(html)中得到数组的输出,php说:htmlentities()期望参数1是字符串,数组给定

这是我的第一个问题。。第二个问题是,我希望数组中的输出相互关联

比如:

number
number
number
number 
.....
但它类似于:输出中的
number

我真的需要这个为我的项目,我有我需要的一切。。只是输出的格式不好

以下是我在控制器中的代码:

$mb_bytes = array();
        foreach ($topTenIp as $val) {
            $bytes_ips[$val] = shell_exec("grep $val /path/file | awk '{print $10}'");
            $add = array_sum(explode("\n", $bytes_ips[$val]));
            $add = $add / 1024 / 1024;
            $add = round($add, 2);
            array_push($mb_bytes, $add);
        }
        if($mb_bytes)
            arsort($mb_bytes);
在$mb_字节中是我想要的数字

这里是我的html:

@foreach($topTenIp as $ip[$i])
   <tr>
       <th>{{ $ip[$i] }}</th>
       <th>{{ here should be the variable mb_bytes }}</th>    
   </tr>
@endforeach
我刚才问过这个问题,得到了很大的帮助,但它仍然不想工作

我希望今天运气好一点:D更新为支持排序数组

首先,您必须以某种方式构造数据,以便来自循环的结果属于IP地址。如果不这样做,则如果对其中一个数组进行排序,它们的顺序将不同。 您可以将IP和字节添加到二维关联数组中,并在该数组上排序:

$ipsWithBytes = [];

foreach ($topTenIp as $val) 
{
    $execResult = shell_exec("grep $val /path/file | awk '{print $10}'");
    $add = array_sum(explode("\n", $execResult));
    $add = $add / 1024 / 1024;
    $add = round($add, 2);

    $ipsWithBytes[] = [  // use the [] notation instead of array_push()
        'ip' => $val,
        'bytes' => $add,
    ];
}

uasort($ipsWithBytes, function($a, $b) { // sort the array descending
    if ($a['bytes'] == $b['bytes'])
    {
        return 0;
    }
    elseif ($a['bytes'] < $b['bytes'])
    {
        return 1;
    }
    else
    {
        return -1;
    }
});

return view('my_view', compact('ipsWithBytes'));
$ipsWithBytes=[];
foreach($topTenIp作为$val)
{
$execResult=shell_exec(“grep$val/path/file | awk'{print$10}'”;
$add=数组_和(分解(“\n”,$execResult));
$add=$add/1024/1024;
$add=四舍五入($add,2);
$ipsWithBytes[]=[//使用[]符号代替数组_push()
“ip”=>$val,
“字节数”=>$add,
];
}
uasort($ipsWithBytes,函数($a,$b){//对数组进行降序排序
如果($a['bytes']=$b['bytes'])
{
返回0;
}
elseif($a['bytes']<$b['bytes']))
{
返回1;
}
其他的
{
返回-1;
}
});
返回视图('my_view',compact('ipsWithBytes');
请注意,使用PHP 7时,可以使用新的spaceship操作符对数组进行排序:

uasort($ipsWithBytes, function($a, $b) {
    return $b['bytes'] <=> $a['bytes']; // $b first, so it sorts descending
});
uasort($ipsWithBytes,函数($a,$b){
首先返回$b['bytes']$a['bytes'];//$b,因此按降序排序
});
最后,您的刀片应该如下所示:

@foreach($ipsWithBytes as $item)
    <tr>
        <td>{{ $item['ip'] }}</td>
        <td>{{ $item['bytes'] }}</td>
    </tr>
@endforeach
@foreach($ipsWithBytes作为$item)
{{$item['ip']}
{{$item['bytes']}
@endforeach
要澄清htmlentities错误的含义:


blade中的双花括号被编译为
。htmlentities()要求参数1的类型为string,但在您的情况下,
$var
的类型为array。

我看不到您正在将数组从控制器传递到视图。控制器代码的其余部分在哪里?如果您在这里发布“Laravel”给出的错误,也会很有帮助。Laravel正好给出了以下错误代码:htmlentities()希望参数1是字符串,给定数组-它意味着mb_字节变量:)--我更新了如何将数组传递给视图的问题:)--代码的另一部分不需要,因为还有很多其他随机的东西:)哈哈!圣母。。。我试了这么多,但都没用xD。。。。。非常感谢您:)mb_字节与IP变量的长度相同。。但是我不明白为什么它不再排序了:如果我在控制器中打印变量,它就被排序了。。但在我看来,它不再是。。也许你明白为什么了吧?谢谢你指出你对$mb_字节进行排序:P这是个问题,因为在你的例子中,$mb_字节中的值在排序后不再属于它们的IP。我将在一分钟内更新我的答案,其中包括这一点。但IP也被排序:oanyways。。我也会尝试一些东西。。如果我做不到,我希望看到你编辑了你的答案:)我非常感谢你的帮助
@foreach($ipsWithBytes as $item)
    <tr>
        <td>{{ $item['ip'] }}</td>
        <td>{{ $item['bytes'] }}</td>
    </tr>
@endforeach