0

Hi guys, I am struggling to return this json file as an array of individual list such as all countries "name", "iso_code" or "border". I can make it return the whole array from each country like;

0: geometry: {type: "MultiPolygon", coordinates: Array(3)} properties: {name: "Bahamas", iso_a2: "BS", iso_a3: "BHS", iso_n3: "044"} type: "Feature" [[Prototype]]: Object

1: geometry: {type: "MultiPolygon", coordinates: Array(30)} properties: {name: "Canada", iso_a2: "CA", iso_a3: "CAN", iso_n3: "124"}

but my aim is to just get individual ones like;

name: [ Bahamas, Canada, Denmark.... ] and iso_a3: [ BHS, CAN.... ]

I have tried many different ways of using for and foreach loops but all seems to return just one object like this;

name: "Vanuatu"

Please can you help me

ini_set('display_errors', 'On');
error_reporting(E_ALL);

$executionStartTime = microtime(true); 

// Read JSON file
$json = file_get_contents('countryBorders.geo.json');

//Decode JSON
$json_data = json_decode($json,true);

//Print data

$countryBorders = $json_data["features"];   
$output["countryBorders"] = $countryBorders; // <-- returns the whole array 

$name = $countryBorders;    

for ($i = 0; $i < count($name); $i++) {     // <-- trying to return individual array 
    $output["name"] = $name[$i]["properties"]["name"];
}

// print_r($json_data);

header('Content-Type: application/json; charset=UTF-8');

echo json_encode($output); 
Coder_k
  • 1
  • 3
  • If you need an array of stuff, then you need to *append to an array*: `$arr[] = $value`. – deceze Aug 22 '21 at 08:41
  • ok your answer was closer at first but now I've got the precise answer $justName[] = $name[$i]["properties"]["name"]; and then $output["name"] = $justName; – Coder_k Aug 22 '21 at 09:20

0 Answers0