php - 拉维尔 5.1 : Command error [Error exception] Illegal offset type

标签 php arrays multidimensional-array foreach laravel-5.1

我正在为 laravel 5.1 编写一个自定义命令,当我运行它时它只是说: 【错误异常】非法偏移类型 这是我的代码:

namespace App\Console\Commands;

use Illuminate\Console\Command;

class InsertDefaultData extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'data:default';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Used for inserting the default data';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $data = array(
                [0] => array(
                        ['name'] => 'Edit',
                        ['slug'] => 'edit',
                        ['view'] => 'admin.edit'
                    ),
                [1] => array(
                        ['name'] => 'Statistics',
                        ['slug'] => 'statistics',
                        ['view'] => 'admin.statistics'
                    ),
                [2] => array(
                        ['name'] => 'Settings',
                        ['slug'] => 'settings',
                        ['view'] => 'admin.settings'
                    ),
                [3] => array(
                        ['name'] => 'Media',
                        ['slug'] => 'media',
                        ['view'] => 'admin.media'
                    )
            );

        foreach ($data as $key => $data) {
            DB::insert('INSERT INTO dashboard_sites (id, name, slug, view) VALUES (NULL, ?, ?, ?)', [$data[$key]['name'], $data[$key]['slug'], $data[$key]['view']]);
        }

        $data = array(
                [0] => array(
                        ['name'] => 'Edit',
                        ['text'] => 'Edit',
                        ['link'] => '/admin/dashboard/edit',
                        ['order'] => 1
                    ),
                [1] => array(
                        ['name'] => 'Statistics',
                        ['text'] => 'Statistics',
                        ['link'] => '/admin/dashboard/statistics',
                        ['order'] => 3
                    ),
                [2] => array(
                        ['name'] => 'Media',
                        ['text'] => 'Media',
                        ['link'] => '/admin/dashboard/media',
                        ['order'] => 2
                    ),
                [3] => array(
                        ['name'] => 'Settings',
                        ['text'] => 'Settings',
                        ['link'] => '/admin/dashboard/settings',
                        ['order'] => 4
                    )
            );

        foreach ($data as $key => $data) {
            DB::insert('INSERT INTO dashboard_menu (id, name, text, link, order) VALUES (NULL, ?, ?, ?, ?)', [$data[$key]['name'], $data[$key]['text'], $data[$key]['link'], $data[$key]['order']]);
        }

    }
}

我不希望它遍历多维数组,然后将数据插入我的数据库,但是当我运行它时它只返回错误。 你能帮我正确运行它吗?

最佳答案

看起来您将数组键包装在 [] 中,php 在数组中进行解释。您不能将数组用作偏移量/数组键。

只需使用:

0 => array( 'name' => 'Edit',...
1 => array(...

你应该很好。

关于php - 拉维尔 5.1 : Command error [Error exception] Illegal offset type,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33588642/

相关文章:

java - 如果数字在数组内,则删除索引

linux - 段错误(核心转储)代码显示此错误

c - C函数中的多维char数组

php - 在 Laravel 中构建搜索算法的技巧

php - 记录有意切换失败的最佳方法是什么?

PHP静态成员不保值

php 数组和 str_replace 一起工作

php - jQuery 提交前确认

c++ - void* 或 char* 用于通用缓冲区表示?

c - 将非动态二维数组传递给 C 中的函数?