Home > Software design >  How to get an array with states as key and cities as value from the json array in PHP
How to get an array with states as key and cities as value from the json array in PHP

Time:05-11

I have a json array which is somethig like this:

[
    {
        "CITY": "Horseheads",
        "STATE": "NY"
    },
    {
        "CITY": "Auburn",
        "STATE": "AL"
    },
    {
        "CITY": "Hurst",
        "STATE": "TX"
    },
    {
        "CITY": "Mesquite",
        "STATE": "TX"
    },
    {
        "CITY": "Arlington",
        "STATE": "TX"
    }
]

I want to create an array out of this which has states abbr as key and cities as values of that key. Example is below:

Array (
    [NY] => Array(
        Horseheads
    )
    [AL] => Array(
        Auburn
    )
    [TX] => Array(
        Hurst,
        Mesquite
        Arlington
    )
)

So far, what I've tried is, I've creted 2 separate arrays as cities and staes and combined them as shown below:

$cities = array_column($jsonArray, 'CITY');
$states = array_column($jsonArray, 'STATE');

$newArray = array_combine($states,$cities);

Which gives me results like this:

Array (
    [NY] => Horseheads
    [AL] => Auburn
    [TX] => Hurst
)

I am unable to get the result I want. Is it possible? Any help, hints or suggestions are appreciated.

Thanks

CodePudding user response:

You can use the below code:

$json_arr = '[
    {
        "CITY": "Horseheads",
        "STATE": "NY"
    },
    {
        "CITY": "Auburn",
        "STATE": "AL"
    },
    {
        "CITY": "Hurst",
        "STATE": "TX"
    },
    {
        "CITY": "Mesquite",
        "STATE": "TX"
    },
    {
        "CITY": "Arlington",
        "STATE": "TX"
    }
]';



$res = json_decode($json_arr, true);


foreach($res as $row)
{
    $tmpArr[$row['STATE']][] = $row['CITY'];
}

print_r($tmpArr);

CodePudding user response:

A simple loop will do that quite easily

$json_string = '[
    {"CITY": "Horseheads","STATE": "NY"},
    {"CITY": "Auburn","STATE": "AL"},
    {"CITY": "Hurst","STATE": "TX"},
    {"CITY": "Mesquite","STATE": "TX"},
    {"CITY": "Arlington","STATE": "TX"}
]';

// convert the JSON String into a PHP data type, 
// an array of objects in this case
$array = json_decode($json_string);

Initialise a new array to hold the result of your processing
$new = [];

// Loop over the array to get one object at a time
foreach ( $array as $obj){
    // add the bits of that object into you new array
    // making the State code the key will create an array of statecodes
    // using the [] in `$new[ $obj->STATE ][]` will add a new entry to the 
    // sub array under that state code when duplicate state codes are seen
    $new[ $obj->STATE ][] = $obj->CITY;
}

print_r($new);

RESULT

Array
(
    [NY] => Array
        (
            [0] => Horseheads
        )
    [AL] => Array
        (
            [0] => Auburn
        )
    [TX] => Array
        (
            [0] => Hurst
            [1] => Mesquite
            [2] => Arlington
        )
)

CodePudding user response:

You can try this

    <?php

$json_array =json_decode('[
    {
        "CITY": "Horseheads",
        "STATE": "NY"
    },
    {
        "CITY": "Auburn",
        "STATE": "AL"
    },
    {
        "CITY": "Hurst",
        "STATE": "TX"
    },
    {
        "CITY": "Mesquite",
        "STATE": "TX"
    },
    {
        "CITY": "Arlington",
        "STATE": "TX"
    }
]',true);

function writeMsg($json_array) {
    $result = [];
    foreach($json_array as $key => $value){
        $result[$value['STATE']] = $value['CITY'] ;
    }
    
    var_dump($result);
}
writeMsg($json_array);
  • Related