Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/281.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 将变量指定给数组(错误)_Php_Laravel_Laravel 5 - Fatal编程技术网

Php 将变量指定给数组(错误)

Php 将变量指定给数组(错误),php,laravel,laravel-5,Php,Laravel,Laravel 5,我正在为我在blade文件中声明的数组分配一个变量。它返回的错误为 ErrorException in 4c73f52d03e3e1331e7b4c0289cafb82dfc22253.php line 127: Undefined offset: 0 (View: /var/www/myteam/resources/views/servicedetails.blade.php) 下面是我的代码: <?php $i = 0 ?> @foreach($var as $v) <?

我正在为我在blade文件中声明的数组分配一个变量。它返回的错误为

ErrorException in 4c73f52d03e3e1331e7b4c0289cafb82dfc22253.php line 127:
Undefined offset: 0 (View:
/var/www/myteam/resources/views/servicedetails.blade.php)
下面是我的代码:

<?php $i = 0 ?>
@foreach($var as $v)
<?php $ary = array(); ?>
    <tr>
        <td name="attrname"><b>{{ $v->service_attribute_name }}</b><br><br></td>
        <td>: <a><input contenteditable="true" name="attrvalue" value="{{ $v->service_attribute_value}}"></a> <br></td>
        <?php $v->service_attribute_value = $ary[$i] ?>
        <?php $i++ ?>
    </tr>
@endforeach

@foreach(var为$v)
{{$v->service_属性_name}


@endforeach

我无法理解这个错误。谢谢大家!

首先,如果在
foreach
循环中声明了
$ary=array()
,则添加到该循环中的任何值都将被循环的下一次迭代覆盖,因此该数组将仅向您提供循环的最后一次迭代的值

其次,我认为你的问题在于这一行:

<?php $v->service_attribute_value = $ary[$i] ?>

如果试图设置数组值,则需要颠倒此语句的顺序:

<?php $ary[$i] = $v->service_attribute_value ?>


希望对你有帮助

首先,如果在
foreach
循环中声明了
$ary=array()
,则添加到该循环中的任何值都将被循环的下一次迭代覆盖,因此该数组将仅向您提供循环的最后一次迭代的值

其次,我认为你的问题在于这一行:

<?php $v->service_attribute_value = $ary[$i] ?>

如果试图设置数组值,则需要颠倒此语句的顺序:

<?php $ary[$i] = $v->service_attribute_value ?>


希望对你有帮助

您的代码应该是这样的:

<?php 
     $i = 0;
     $ary = array();
?>
@foreach($var as $v)
    <tr>
        <td name="attrname"><b>{{ $v->service_attribute_name }}</b><br><br></td>
        <td>: <a><input contenteditable="true" name="attrvalue" value="{{ $v->service_attribute_value}}"></a> <br></td>
        <?php 
             $ary[$i] = $v->service_attribute_value;
             $i++; 
        ?>
    </tr>
@endforeach

@foreach(var为$v)
{{$v->service_属性_name}


@endforeach
您的代码应该是这样的:

<?php 
     $i = 0;
     $ary = array();
?>
@foreach($var as $v)
    <tr>
        <td name="attrname"><b>{{ $v->service_attribute_name }}</b><br><br></td>
        <td>: <a><input contenteditable="true" name="attrvalue" value="{{ $v->service_attribute_value}}"></a> <br></td>
        <?php 
             $ary[$i] = $v->service_attribute_value;
             $i++; 
        ?>
    </tr>
@endforeach

@foreach(var为$v)
{{$v->service_属性_name}


@endforeach
在foreach循环之前声明数组
$ary
。在foreach循环之前,使用此
声明数组
$ary
更改此行
。用此
更改此行