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