如何在PHP中格式化动态生成的嵌套数组

时间:2017-10-14 17:11:30

标签: php arrays loops nested podscms

我试图通过使用Pods框架从数据库中提取多边形数据来动态生成嵌套数组,但是我很难让格式/语法正确(我不是专家)谈到PHP)。

这是我的代码,其中" polygon_latlon"是包含双引号内的纬度和经度点对的字段,由逗号分隔,构成(不规则)多边形:

$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);

以这种格式使用print_r($ lines)输出:

 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
);

以这种格式使用print_r($ polygons1)输出:

 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

我需要输出什么,但我无法找到正确的代码/语法,是:

 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
);

即。我需要嵌套在另一个数组中的print_r($ polygons1)的输出。我怎么做到这一点?

最终,我不想打印嵌套数组,而是将其存储为变量,以便在另一部分代码中使用。

1 个答案:

答案 0 :(得分:0)

抱歉,我迟到了,但您可以使用以下代码进行操作,

$var = array($polygons1)