Конвертировать массив PHP в массив javascript, используя модель - PullRequest
0 голосов
/ 12 января 2020

У меня есть следующий массив javascript, который мне нужно воспроизвести в 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" }]},

                ]

Что я пытался сделать в 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 виде не соответствует ожидаемому. Пожалуйста, смотрите ниже:

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"}}]]

Кто-нибудь знает, где я не прав? Спасибо!

1 Ответ

0 голосов
/ 12 января 2020

Проблема решена, Массив должен быть таким (после добавления вопроса я понял, что он проще):

$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" ]]],
];
...