PHP - syntax for list of arrays in a single variable

senectus

I have a very complex array which contains many other very complex nested arrays. I want to break up some of the inside array groups into separate variables outside of the main variable for readability, however I'm not sure what the correct syntax is for having a list of multiple arrays in a single variable.

Wrapping the list of arrays in an array while declaring the variable isn't an option, as the variable will be inserted inside the wrapper array later on in the $array variable, so the list of arrays need to be on their own and not inside an array in the $sub_arrays variable.

<?php

$sub_arrays = array(), array(), array(); // syntax error

$array = array(
    'template' => array(
        array(
            array(),
            array(
                array(),
                // syntax error
                $sub_arrays,

                // this works, but need to be declared outside
                // array(), array(), array(),
                array(),
            )
        ),
    ),
);

print_r($array);

https://3v4l.org/2EvRA

Alternatively, separating some of the sub arrays into another .php file and using include() and return would also be an option instead of the variable? But I'm not sure about the syntax of that either.

Separation would be good not just for readability, but in case a particular group of arrays need to be reused in another instance as well.

The data is a WordPress block editor (Gutenberg) template with a lot of nested group and column blocks inside other blocks. https://developer.wordpress.org/block-editor/developers/block-api/block-templates/#nested-templates

Is this even possible to do in PHP?

Florent Cardot

Could you try this:

<?php

$sub_arrays = array(array(), array(), array()); // no syntax error

$array = array(
    'template' => array(
        array(
            array(),
            array(
                array_merge(
                array(array()),
                $sub_arrays),
                 array(),
            )
        ),
    ),
);

print_r($array);

To make it work, you have to merge your "outsider" array with the existing one. The existing one must be encapsulated in an array

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

PHP debugging to list change of datatype of a single variable

How to save a list variable with multiple arrays to a single *.dat file in python?

Alter single array's value within list of arrays in PHP

Syntax or Variable error in PHP

Convert single integer variable to a list of numbers (for example +/-3) with PHP

Convert php arrays with the new syntax

Convert a series of arrays to a single list

What is the correct syntax for creating a list of a list of arrays?

PHP variable Syntax using _GET

PHP Syntax of an href with row variable

Combine Multiple Arrays + Variable into single JSON object

Correct PHP syntax for array that contains associative arrays

Having two arrays in variable php

List of Arrays to single Dictionary using LINQ

PHP store a single variable on the server?

JSON response to single php variable

How to convert a list of numpy arrays with empty arrays into a single array?

PHP combine multiple arrays into one single entity

PHP - Return single row from associated arrays

Merging Two Arrays in to a single grouped array PHP

Php top list from arrays

PowerShell, Pulling out multiple arrays from an variable to make a single variable

Merge two arrays into single arrays in php laravel and displaying in blade

Change list to list of associative arrays in PHP

Pandas - Convert list of numpy arrays into one single list?

PHP syntax error at the end of Wordpress single.php Loop

Strange PHP Syntax - String before $variable? What is this?

mysql search query syntax using a php $variable

php: What good syntax for integrating a variable into regex