将变量分配给数组(错误)

时间:2017-04-04 02:46:59

标签: php laravel laravel-5

我将变量分配给我在刀片文件中声明的数组。它返回错误

var zip = new JSZip(), config = {...}
localforage.iterate(function(...) {
    // do stuff in loop
}).then(function(result) {
   $.getScript("/my/precompiled/template.js")
   .then(function(obj) {
      // build files based on scope imported inside template.js
      zip.file("file.js", Handlebars.templates["js"](config));
      zip.file("file.css", Handlebars.templates["css"](config));
      zip.file("file.html", Handlebars.templates["html"](config));
   })
   .then(function(obj) {
      // get a file based on a config variable previously set somewhere
      fetch("/" + config.somevalue + "/prepackaged.zip")
      .then(function(response) {
          return response.arrayBuffer();
      })
      .then(function(ab) {
         // add a file based on a template named after that config variable
         zip.file("somefile.txt", Handlebars.templates[config.somevalue](config));
         zip.loadAsync(ab)
         .then(function(inst) {
             zip.generateAsync({type:"blob"})
             .then(function(content) {
                // ... download as zip
             });
         });
      })

以下是我的代码:

ErrorException in 4c73f52d03e3e1331e7b4c0289cafb82dfc22253.php line 127:
Undefined offset: 0 (View:
/var/www/myteam/resources/views/servicedetails.blade.php)

我无法理解错误。谢谢!

2 个答案:

答案 0 :(得分:0)

首先,如果在JSON循环内声明{ "config": { "connections": [ { "cfgProperty": { "Env": "local", "Host": "localhost", "Port": "8001", "Directory": "/apps/workspace/hade/maps/src01_cvs" }, "connectionName": "src01_cvs" }, { "cfgProperty": { "Env": "local", "Host": "localhost", "Port": "8001", "Directory": "/apps/workspace/hade/maps/trg01_cvs" }, "connectionName": "trg01_cvs" } ] } } ,则添加到它的任何值都将被循环的下一次迭代覆盖,因此数组只会为您提供循环的最后一次迭代的值。

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

$ary = array()

如果您正在尝试设置数组值,则需要反转此语句的顺序:

foreach

希望这有用!

答案 1 :(得分:0)

您的代码应该是这样的:

<?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