1

I tried real hard for my title to make sense haha. I have this 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
    }
}]

As you can see the position of the JSON object in the global json serves as the name of the object and I don't want this. This is what I want:

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

I want the objects without a name. This is the code I'm using:

$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

Thank you

11
  • and what result do you get with your code? Or is the first json the result of your code right now? Commented Jan 2, 2018 at 21:21
  • I think the problem lies with converting the $data into an array before encoding, but If i remove array() the json objects won't be inside an array and I also don't want that Commented Jan 2, 2018 at 21:23
  • @Jeff it is the first json Commented Jan 2, 2018 at 21:23
  • as we don't know the structure of the input (='releases.json') it's hard to answer, apart from guessing... Commented Jan 2, 2018 at 21:29
  • Are you sure your not using JSON_FORCE_OBJECT? Commented Jan 2, 2018 at 21:30

1 Answer 1

3

You need to use array_values() to reindex the array.

PHP's json_encode() function will only produce an array if all array keys are numeric and don't have any gaps, e.g. 0, 1, 2, 3 etc. The problem is that array_filter() can remove certain keys and leave gaps, which causes json_encode() to include the keys as you show in your example. You can fix this by using array_values() to re-index the array before calling json_encode().

Here is an example:

<?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"]
Sign up to request clarification or add additional context in comments.

Comments