{"type":"FeatureCollection","features":[{"type":"Feature","id":"156","geometry":{"type":"Polygon","coordinates":"[ [ [ -520.219, -1223.00 ], [ -26.2188, -1227.00 ], [ -26.2188, -81.0000 ], [ -520.219, -81.0000 ], [ -520.219, -1223.00 ] ] ]"},"properties":{"description":"401","fill":"#ff931e","fill-opacity":0.3,"stroke":"#e6761b","stroke-width":"2","stroke-opacity":0.9}},{"type":"Feature","id":"157","geometry":{"type":"Polygon","coordinates":"[[[-3546.84, 2290.81], [-2390.84, 2290.81], [-2394.84, 1790.81], [-3542.84, 1790.81], [-3546.84, 2290.81]]]"},"properties":{"description":"404","fill":"#ff931e","fill-opacity":0.3,"stroke":"#e6761b","stroke-width":"2","stroke-opacity":0.9}}]}
$fullJSON = json_decode( $jsonString, true );
$completeJSON = array_map( function ( $feature ) {
$feature['geometry']['coordinates'] = json_decode( $feature['geometry']['coordinates'], true );
return $feature;
}, $fullJSON['features'] );
Array
(
[0] => Array
(
[type] => Feature
[id] => 156
[geometry] => Array
(
[type] => Polygon
[coordinates] => Array
(
[0] => Array
(
[0] => Array
(
[0] => -520.219
[1] => -1223
)
[1] => Array
(
[0] => -26.2188
[1] => -1227
)
[2] => Array
(
[0] => -26.2188
[1] => -81
)
[3] => Array
(
[0] => -520.219
[1] => -81
)
[4] => Array
(
[0] => -520.219
[1] => -1223
)
)
)
)
[properties] => Array
(
[description] => 401
[fill] => #ff931e
[fill-opacity] => 0.3
[stroke] => #e6761b
[stroke-width] => 2
[stroke-opacity] => 0.9
)
)
[1] => Array
(
[type] => Feature
[id] => 157
[geometry] => Array
(
[type] => Polygon
[coordinates] => Array
(
[0] => Array
(
[0] => Array
(
[0] => -3546.84
[1] => 2290.81
)
[1] => Array
(
[0] => -2390.84
[1] => 2290.81
)
[2] => Array
(
[0] => -2394.84
[1] => 1790.81
)
[3] => Array
(
[0] => -3542.84
[1] => 1790.81
)
[4] => Array
(
[0] => -3546.84
[1] => 2290.81
)
)
)
)
[properties] => Array
(
[description] => 404
[fill] => #ff931e
[fill-opacity] => 0.3
[stroke] => #e6761b
[stroke-width] => 2
[stroke-opacity] => 0.9
)
)
)
$generate_xml = $_GET['generate_xml']; //просто для проверки
$json_object = $_GET['json_object'];
if($_GET['generate_xml']) {
$dir = $_SERVER['DOCUMENT_ROOT'].'/scheme/geoObjects2.geojson';
$write = file_put_contents( $dir , $json_object );
if($write) { $info = "Запись прошла";} else {$info = "Запись не прошла";}
echo $info;
}
как мне строку превратить в массив правильно
строка - [[[-3546.84, 2290.81], [-2390.84, 2290.81], [-2394.84, 1790.81], [-3542.84, 1790.81], [-3546.84, 2290.81]]]
<?php
$str = "[[[-3546.84, 2290.81], [-2390.84, 2290.81], [-2394.84, 1790.81], [-3542.84, 1790.81], [-3546.84, 2290.81]]]";
$array = array(
'coordinates' => $str
);
$json = json_encode($array, true);
echo $json;
$array_de = json_decode($json);
echo $array_de;
?>
{"coordinates":"[[[-354684, 229081], [-239084, 229081], [-239484, 179081], [-354284, 179081], [-354684, 229081]]]"}<br />
<b>Fatal error</b>: Uncaught Error: Object of class stdClass could not be converted to string in [...][...]:11
Stack trace:
#0 {main}
thrown in <b>[...][...]</b> on line <b>11</b><br />
<?php
$str = "[[[-3546.84, 2290.81], [-2390.84, 2290.81], [-2394.84, 1790.81], [-3542.84, 1790.81], [-3546.84, 2290.81]]]";
$array = array(
'coordinates' => json_decode($str);
);
$json = json_encode($array, true);
echo $json;
[[[[-3546.84,2290.81],[-2390.84,2290.81],[-2394.84,1790.81],[-3542.84,1790.81],[-3546.84,2290.81]]]]
<?
$mass2 = array("type" =>"Feature","id" => "",
"geometry" => array("type" => "Polygon","coordinates" => 1), //сюда надо вставаить координаты
"properties" => array(
"description" => "",
"fill" => "#ff931e",
"fill-opacity"=>0.3,
"stroke"=>"#e6761b",
"stroke-width"=>"2",
"stroke-opacity"=>0.9
),
);
$mass3 = array(
'type' => "FeatureCollection",
'features'=> "",
);
$featuresMass=[];
\Bitrix\Main\Loader::includeModule('iblock');
$arSelect = Array("ID", "IBLOCK_ID", "NAME", "DATE_ACTIVE_FROM",);
$arFilter = Array("IBLOCK_ID"=>11, "ACTIVE_DATE"=>"Y", "ACTIVE"=>"Y");
$res = CIBlockElement::GetList(Array(), $arFilter, false, Array("nPageSize"=>10), $arSelect);
//$mass=[];
$map = 0;
$map2 = 0;
$map3 = 0;
while($ob = $res->GetNextElement()){
$arFields = $ob->GetFields();
$arProps = $ob->GetProperties();
$str = $arProps['COORDINATES']['VALUE']; //координаты
$array = array(
'coordinates' => json_decode($str)
);
$json_ne = json_encode($array, true);
//echo $json_ne;
$mass2['id']=$arFields["ID"];
$mass2['properties']['description']=$arFields["NAME"];
$mass2['geometry']['coordinates']=$json_ne;
echo '<pre>';
//print_r($mass2);
//print_r($arProps);
echo '</pre>';
array_push($featuresMass,$mass2);
}
$data_for_write ='';
$mass3['features'] = $featuresMass;
$json = json_encode($mass3);
?>