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);
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?
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.
Comments