技术求助:如何使用变量扩展PHP关联数组
Hey Henning, let's break down what's causing the issue and get your code working properly!
The Root Problem
When you tried to add $wareHouseList directly at the end of your sections array definition, you hit a PHP syntax error. You can't just insert an array variable directly into another array's literal syntax like that—PHP doesn't recognize this as valid, which is why your structure check fails and you get no output.
Solution 1: Use array_merge() (Works for All PHP Versions)
You can merge your original section array with $wareHouseList using array_merge(), which combines the two arrays into one:
// First build your warehouse list with dynamic IDs (fix for hardcoded 105) foreach($this->getWarehouseListForm() as $wareHouse) { // Replace with the actual ID field from your $wareHouse object/array $warehouseId = $wareHouse['id']; $wareHouseList[] = [ "title" => "wareHouse[$warehouseId]", "form" => [ "storeId[$warehouseId]" => [ "type" => "inputText", "options" => [ "name" => "TSL", ], ], ], ]; } // Merge sections correctly $config = [ "sections" => array_merge( [ [ "title" => 'Schuhe24Assistant.ftpServerTitle', "description" => 'Schuhe24Assistant.ftpServerDescription', "form" => [ "ftpServer" => [ 'type' => 'text', 'defaultValue' => 'ftp.hisasp2.com', 'options' => [ 'name' => 'Schuhe24Assistant.ftpServer', 'required' => true, ] ], "ftpUser" => [ 'type' => 'text', 'defaultValue' => 'username', 'options' => [ 'name' => 'Schuhe24Assistant.ftpUser', 'required' => true, ] ], "ftpPassword" => [ 'type' => 'text', 'defaultValue' => 'password', 'options' => [ 'name' => 'Schuhe24Assistant.ftpPassword', // 'isPassword' => true, 'required' => true, ], ], "deleteFiles" => [ 'type' => 'toggle', 'defaultValue' => true, 'options' => [ 'name' => 'Schuhe24Assistant.deleteFiles', ], ], ], ], ], $wareHouseList ), ];
Solution 2: Use Array Spread Operator (PHP 7.4+)
If you're running PHP 7.4 or newer, you can use the cleaner spread operator (...) to unpack $wareHouseList directly into the sections array:
// Build $wareHouseList with dynamic IDs as above $config = [ "sections" => [ [ "title" => 'Schuhe24Assistant.ftpServerTitle', "description" => 'Schuhe24Assistant.ftpServerDescription', "form" => [ // ... your existing FTP form fields ], ], // Unpack each warehouse section into the main sections array ...$wareHouseList ], ];
Bonus Fix: Stop Hardcoding IDs!
I replaced the hardcoded 105 with $warehouseId from your loop variable. Right now, every entry in $wareHouseList would have identical keys, which would cause conflicts with multiple warehouses. Using the actual ID from $wareHouse ensures each entry is unique and correctly linked to its warehouse.
Why This Works
Both methods properly combine your original FTP section with all the warehouse sections from $wareHouseList, creating a single valid array structure that passes your checks and outputs correctly.
内容的提问来源于stack exchange,提问作者Henning Möllendorf

