简体   繁体   中英

how to format dynamically-generated nested array in PHP

I'm trying to dynamically generate a nested array by pulling polygon data from a database using the Pods Framework, however I'm struggling to get the formatting / syntax right (I'm not an expert when it comes to PHP).

This is my code, where "polygon_latlon" is the field containing pairs of latitude and longitude points within double quotes separated by commas which make up the (irregular) polygons:

$lines = array();

//set find parameters
$params = array(
    'limit' => -1
);
//get pods object
 $maps = pods( 'maps_osexplorer', $params );
//loop through records
 if ( $maps->total() > 0 ) {
 while ( $maps->fetch() ) {

 $polygon1 = $maps->field('polygon_latlon');

 $lines[] = "array($polygon1)";

}

}

 $polygons1 = implode(',', $lines);

Using print_r($lines) outputs in this format:

 Array (
[0] => array(“54.8209937901589 -5.1847118669456265″,”54.82875805834827 -4.873857026861909″,”55.03518041767634 -4.888625345496792″,”55.02735680301253 -5.201071164745328″,”54.8209937901589 -5.1847118669456265”)
[1] => array(“54.73307781328839 -5.107888813651375″,”54.73542913841878 -5.014843407073816″,”54.62773859547211 -5.006866177191955″,”54.63292697997008 -4.790281966946296″,”54.839371033206184 -4.804517368244679″,”54.831783194724316 -5.115474120227528″,”54.73307781328839 -5.107888813651375”)
[2] => array(“54.66161594261056 -4.714709589503054″,”54.66230647443063 -4.683739691903676″,”54.68026002182563 -4.684924716676078″,”54.679569033955396 -4.715908247171228″,”54.66161594261056 -4.714709589503054”)
[3] => array(“54.79285752469713 -4.871307801664465″,”54.799816987439904 -4.560586644307456″,”55.06913330516994 -4.577766586876797″,”55.06210435522974 -4.890565603716853″,”54.79285752469713 -4.871307801664465”)
[4] => array(“54.76588200465172 -4.675046760686505″,”54.767570233715865 -4.597418172482791″,”54.66433060233055 -4.590822213183797″,”54.66967882431423 -4.327495656368091″,”54.93904807902748 -4.34303745757995″,”54.93194793898191 -4.686064981190482″,”54.76588200465172 -4.675046760686505”)
 etc
 etc
);

Using print_r($polygons1) outputs in this format:

 array(“54.8209937901589 -5.1847118669456265″,”54.82875805834827 -4.873857026861909″,”55.03518041767634 -4.888625345496792″,”55.02735680301253 -5.201071164745328″,”54.8209937901589 -5.1847118669456265”),
 array(“54.73307781328839 -5.107888813651375″,”54.73542913841878 -5.014843407073816″,”54.62773859547211 -5.006866177191955″,”54.63292697997008 -4.790281966946296″,”54.839371033206184 -4.804517368244679″,”54.831783194724316 -5.115474120227528″,”54.73307781328839 -5.107888813651375”),
 array(“54.66161594261056 -4.714709589503054″,”54.66230647443063 -4.683739691903676″,”54.68026002182563 -4.684924716676078″,”54.679569033955396 -4.715908247171228″,”54.66161594261056 -4.714709589503054”),
 array(“54.79285752469713 -4.871307801664465″,”54.799816987439904 -4.560586644307456″,”55.06913330516994 -4.577766586876797″,”55.06210435522974 -4.890565603716853″,”54.79285752469713 -4.871307801664465”),
 array(“54.76588200465172 -4.675046760686505″,”54.767570233715865 -4.597418172482791″,”54.66433060233055 -4.590822213183797″,”54.66967882431423 -4.327495656368091″,”54.93904807902748 -4.34303745757995″,”54.93194793898191 -4.686064981190482″,”54.76588200465172 -4.675046760686505”),
 etc
 etc

What I need it to output, but I can't quite find the right code / syntax for, is:

 array(
 array(“54.8209937901589 -5.1847118669456265″,”54.82875805834827 -4.873857026861909″,”55.03518041767634 -4.888625345496792″,”55.02735680301253 -5.201071164745328″,”54.8209937901589 -5.1847118669456265”),
 array(“54.73307781328839 -5.107888813651375″,”54.73542913841878 -5.014843407073816″,”54.62773859547211 -5.006866177191955″,”54.63292697997008 -4.790281966946296″,”54.839371033206184 -4.804517368244679″,”54.831783194724316 -5.115474120227528″,”54.73307781328839 -5.107888813651375”),
 array(“54.66161594261056 -4.714709589503054″,”54.66230647443063 -4.683739691903676″,”54.68026002182563 -4.684924716676078″,”54.679569033955396 -4.715908247171228″,”54.66161594261056 -4.714709589503054”),
 array(“54.79285752469713 -4.871307801664465″,”54.799816987439904 -4.560586644307456″,”55.06913330516994 -4.577766586876797″,”55.06210435522974 -4.890565603716853″,”54.79285752469713 -4.871307801664465”),
 array(“54.76588200465172 -4.675046760686505″,”54.767570233715865 -4.597418172482791″,”54.66433060233055 -4.590822213183797″,”54.66967882431423 -4.327495656368091″,”54.93904807902748 -4.34303745757995″,”54.93194793898191 -4.686064981190482″,”54.76588200465172 -4.675046760686505”),
 etc
 etc
);

ie I need the output from print_r($polygons1) nested within another array. How would I achieve this?

Ultimately I don't want to print the nested array but store it as a variable for use in another section of code.

抱歉,我来晚了,但是您可以使用以下代码来实现,

$var = array($polygons1)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM