【问题标题】:Laravel 5.1: Command error [Error exception] Illegal offset typeLaravel 5.1:命令错误[错误异常]非法偏移类型
【发布时间】:2016-02-08 21:21:22
【问题描述】:

我正在为 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 arrays multidimensional-array foreach laravel-5.1


    【解决方案1】:

    不幸的是,PHP 不允许使用数组作为数组键,正如@Zoe Blair 所说。从字面上看,您需要做的就是删除键周围的括号!

    【讨论】:

      【解决方案2】:

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

      只需使用:

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

      而且你应该很好。

      【讨论】:

        猜你喜欢
        • 2018-01-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-05-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-10-06
        相关资源
        最近更新 更多