如何从 PHP 中的 json 数组中获取以州为键、城市为值的数组

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

我有一个 json 数组,它是这样的:

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

我想以此创建一个数组,其中状态缩写作为键,城市作为该键的值。示例如下:

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

到目前为止,我尝试过的是,我创建了 2 个单独的数组作为 cities 和 staes,并将它们组合如下所示:

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

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

这给了我这样的结果:

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

我无法得到我想要的结果。可能吗?感谢任何帮助、提示或建议。

谢谢

一个简单的循环就可以很容易地做到这一点

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

结果

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

您可以使用以下代码:

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

你可以试试这个

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