簡體   English   中英

使用模型將 PHP 數組轉換為 javascript 數組

[英]Convert PHP array to javascript array using model

我有以下需要在 php 中重現的 javascript 數組:

array: [
                    { width: 150, id: "id", type: "string", header: [{ text: "Unit ID" }], editing: false},
                    { width: 150, id: "project_name", header: [{ text: "Project" }, { content: "selectFilter" }], editing: false},
                    { width: 150, id: "block_title",  header: [{ text: "Block" }, { content: "selectFilter" }], editing: false },
                    { width: 150, id: "unit", header: [{ text: "Unit" }, { content: "comboFilter" }] },
                    { width: 150, id: "core", header: [{ text: "Core" }, { content: "inputFilter" }]},
                    { width: 150, id: "floor", header: [{ text: "Floor" }, { content: "inputFilter" }]},
                    { width: 150, id: "unit_type", header: [{ text: "Unit type" }, { content: "inputFilter" }]},

                ]

我在 PHP 中嘗試做的事情:

$array[] = [
    [ 'width' => 150, 'id' => 'id', 'type' => 'string', 'header' => [ 'text' => 'Unit ID' ], 'editing' => false],
    [ 'width' => 150, 'id' => "project_name", 'header' => [ 'text' => "Project", 'content' => "selectFilter"], 'editing' => false],
    [ 'width' => 150, 'id' => "block_title",  'header' => [ 'text' => "Block" , 'content' => "selectFilter" ], 'editing' => false ],
    [ 'width' => 150, 'id' => "unit", 'header' => [ 'text' => "Unit" , 'content' => "comboFilter" ] ],
    [ 'width' => 150, 'id' => "core", 'header' => [ 'text' => "Core" , 'content' => "inputFilter" ]],
    [ 'width' => 150, 'id' => "floor", 'header' => [ 'text' => "Floor", 'content' => "inputFilter" ]],
    [ 'width' => 150, 'id' => "unit_type", 'header' => [ 'text' => "Unit type" , 'content' => "inputFilter" ]],
];

不幸的是{!! json_encode($array) !!} Laravel 視圖中的{!! json_encode($array) !!}不符合預期。 請參閱以下內容:

array: [[{"width":150,"id":"id","type":"string","header":{"text":"Unit ID"},"editing":false},{"width":150,"id":"project_name","header":{"text":"Project","content":"selectFilter"},"editing":false},{"width":150,"id":"block_title","header":{"text":"Block","content":"selectFilter"},"editing":false},{"width":150,"id":"unit","header":{"text":"Unit","content":"comboFilter"}},{"width":150,"id":"core","header":{"text":"Core","content":"inputFilter"}},{"width":150,"id":"floor","header":{"text":"Floor","content":"inputFilter"}},{"width":150,"id":"unit_type","header":{"text":"Unit type","content":"inputFilter"}}]]

有誰知道我錯在哪里? 謝謝!

問題解決了,數組應該是這樣的(添加問題后我意識到它比那個簡單):

$array = [
    [ 'width' => 150, 'id' => 'id', 'type' => 'string', 'header' => [[ 'text' => 'Unit ID' ]], 'editing' => false],
    [ 'width' => 150, 'id' => "project_name", 'header' => [[ 'text' => "Project"], ['content' => "selectFilter"]], 'editing' => false],
    [ 'width' => 150, 'id' => "block_title",  'header' => [[ 'text' => "Block"], ['content' => "selectFilter" ]], 'editing' => false ],
    [ 'width' => 150, 'id' => "unit", 'header' => [[ 'text' => "Unit"] , ['content' => "comboFilter" ]] ],
    [ 'width' => 150, 'id' => "core", 'header' => [[ 'text' => "Core"], ['content' => "inputFilter" ]]],
    [ 'width' => 150, 'id' => "floor", 'header' => [[ 'text' => "Floor"], ['content' => "inputFilter" ]]],
    [ 'width' => 150, 'id' => "unit_type", 'header' => [[ 'text' => "Unit type"] , ['content' => "inputFilter" ]]],
];

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM