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

时间:2019-01-02 14:05:18

标签: php json

我的代码将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

输出应为imo,不带方括号

{
  "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"
      }
  }
}

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

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

$data = json_decode($json, true);

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

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

2 个答案:

答案 0 :(得分: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个项目作为键。

答案 1 :(得分: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不在乎键是数字还是字符串。