【问题标题】:Remove index based json object name in php [duplicate]在php中删除基于索引的json对象名称[重复]
【发布时间】:2018-06-12 13:45:21
【问题描述】:

我非常努力地使我的标题有意义哈哈。我有这个 JSON:

[{
    "0": {
        "id": 130427,
        "created_at": 1512521776301,
        "updated_at": 1512549188911,
        "category": 0,
        "platform": 6,
        "date": 1513987200000,
        "region": 8,
        "y": 2017,
        "m": 12,
        "human": "2017-Dec-23",
        "game": 76663
    },
    "2": {
        "id": 131795,
        "created_at": 1514172411633,
        "updated_at": 1514190849639,
        "category": 0,
        "platform": 39,
        "date": 1513987200000,
        "region": 8,
        "y": 2017,
        "m": 12,
        "human": "2017-Dec-23",
        "game": 78658
    }
}]

如您所见,JSON 对象在全局 json 中的 position 用作对象的名称,我不希望这样。这就是我想要的:

[{
        "id": 130427,
        "created_at": 1512521776301,
        "updated_at": 1512549188911,
        "category": 0,
        "platform": 6,
        "date": 1513987200000,
        "region": 8,
        "y": 2017,
        "m": 12,
        "human": "2017-Dec-23",
        "game": 76663
    },
    {
        "id": 131795,
        "created_at": 1514172411633,
        "updated_at": 1514190849639,
        "category": 0,
        "platform": 39,
        "date": 1513987200000,
        "region": 8,
        "y": 2017,
        "m": 12,
        "human": "2017-Dec-23",
        "game": 78658
    }
]

我想要没有名称的对象。这是我正在使用的代码:

$json = file_get_contents('./releases.json');
$data = json_decode($json, TRUE);
$region = isset($_GET['region']) ? $_GET['region'] : null;

# if region is not null: ?region=8
if ($region) {
    $region_filter = function($v) use ($region) {
        // 8 == Worldwide
        if ($v['region'] == $region || $v['region'] == 8) {
            return true;
        } else {
            return false;
        }
    };
    $data = array_filter($data['data'], $region_filter);
}

header('Content-Type: application/json');
echo json_encode(array($data)); // good

谢谢

【问题讨论】:

  • 你的代码得到了什么结果?还是第一个 json 是您现在代码的结果?
  • 我认为问题在于在编码之前将 $data 转换为数组,但如果我删除 array() json 对象将不会在数组中,我也不希望这样
  • @Jeff 这是第一个 json
  • 因为我们不知道输入的结构(='releases.json'),所以除了猜测之外很难回答......
  • 您确定不使用JSON_FORCE_OBJECT

标签: php json


【解决方案1】:

您需要使用array_values() 重新索引数组。

PHP 的 json_encode() 函数只会在所有数组键都是数字且没有任何间隙的情况下生成一个数组,例如0、1、2、3 等。问题是array_filter() 可以删除某些键并留下间隙,这导致json_encode() 包含您在示例中显示的键。您可以通过在调用 json_encode() 之前使用 array_values() 重新索引数组来解决此问题。

这是一个例子:

<?php

// numeric array keys with no gaps
$a = ['a', 'b', 'c'];
echo json_encode($a);
// ["a","b","c"]

// filter out the 'b' element to introduce a gap in the keys
$a = array_filter($a, function ($v) {
    return $v !== 'b';
});
echo json_encode($a);
// {"0":"a","2":"c"}

// re-index the array to remove gaps
$a = array_values($a);
echo json_encode($a);
// ["a","c"]

【讨论】:

    猜你喜欢
    • 2017-04-06
    • 2012-07-12
    • 1970-01-01
    • 2014-02-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-28
    相关资源
    最近更新 更多