为什么我的PHP JSON输出编号索引?

问题描述 投票:-2回答:2

我的代码将JSON输出为数组,而不是列表。也许我只是对PHP创建JSON数组或列表的方式感到困惑。

$json = file_get_contents($url);

$json = json_decode($json, true);

$data = $json['data'];

$cities = array();

foreach ($data as $key => $val) {

    $city = $val['city'];
    $region = $val['region'];
    $district = $val['district'];
    $combined = array(
        $city => array(
            array('city' => $city), 
            array('region' => $region), 
            array('district' => $district)
    ));

    if (!in_array($combined, $cities)) {

        array_push($cities, $combined);

    }

}

$result = json_encode(array('cities'=>$cities, 'branches'=>$data), JSON_PRETTY_PRINT|JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES);

产量

{
  "cities": [
    {
      "City 1": [
        {
          "city": "City 1"
        },
        {
          "region": "Region 1"
        },
        {
          "district": "District 1"
        }
      ]
    },
    {
      "City 2": [
        {
          "city": "City 2"
        },
        {
          "region": "Region 2"
        },
        {
          "district": "District 2"
        }
      ]
    },
    ...
  ]
}

如果我然后在创建的JSON文件上使用$cityCount = count($data['cities']);,我会收到0

输出应该是,没有方括号

{
  "cities": {
      "City 1": {
        "city": "City 1",
        "region": "Region 1",
        "district": "District 1"
      },
      "City 2": {
        "city": "City 2",
        "region": "Region 2",
        "district": "District 2"
      },
      "City 3": {
        "city": "City 3",
        "region": "Region 3",
        "district": "District 3"
      },
      "City 4": {
        "city": "City 4",
        "region": "Region 4",
        "district": "District 4"
      }
  }
}

以便我以后可以使用ie。 $data['cities']['City 1']['region']。然后计算城市条目的数量。

$json = file_get_contents($url); // URL of the above output

$data = json_decode($json, true);

$cityCount = count($data['cities']);

当每个数组都定义了键/值对时,为什么输出在数组中创建对象?

php json
2个回答
1
投票

你想要的输出是无效的json。

但是,似乎并不是真的希望您正在寻找:为了按照您的意愿访问数据,您应该更改:

$combined = array(
    $city => array(
        array('city' => $city), 
        array('region' => $region), 
        array('district' => $district)
));

至:

$combined = array(
    $city => array('city' => $city, 
                   'region' => $region, 
                   'district' => $district,
));

现在,您将在第三级将所有3个项目作为键。


0
投票

正确的方法是

$url = 'http://www.something.com/jason.json'; // Get data from public json file

$json = file_get_contents($url);

$json = json_decode($json, true);

$data = $json['data']; // The actual data is in the 'data' object

$cities = array();

foreach ($data as $key => $val) { // City, region, district are nested in numbered objects

    $city = $val['city'];
    $region = $val['region'];
    $district = $val['district'];
    $combined = array(
        'city' => $city, 
        'region' => $region, 
        'district' => $district,
    );

    if (!in_array(array($city => $combined), $cities)) {

        $cities[$city] = $combined; // Add the $combined value array to $cities with the $city as the key

    }

}

$result = json_encode(array('cities'=>$cities), JSON_PRETTY_PRINT|JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES);

使用array_push($cities, $combined);在最终的json中创建带有数字键的对象,而$cities[$city] = $combined;使用$city作为键,从而可以在将来搜索json数据时使用实际的城市名称。

然后保存结果。

// continued

$saveName = 'localdirectory/new.json'; // Let's save the parsed json

file_put_contents($saveName, $result);

最后,我们可以查看我们保存到新文件的城市数量

// continued

$newUrl = 'http://www.example.com/localdirectory/new.json';

$newJson = file_get_contents($newUrl);

$newData = json_decode($newJson, true);

$cityCount = count($data['cities']);

count($data['cities']);也在@ jeroen的答案中工作,count不关心键是数字还是字符串。

© www.soinside.com 2019 - 2024. All rights reserved.