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

Lawrence Cherone
  • 46,049
  • 7
  • 62
  • 106
  • and what result do you get with your code? Or is the first json the result of your code right now? – Jeff Jan 02 '18 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 –  Jan 02 '18 at 21:23
  • @Jeff it is the first json –  Jan 02 '18 at 21:23
  • as we don't know the structure of the input (='releases.json') it's hard to answer, apart from guessing... – Jeff Jan 02 '18 at 21:29
  • Are you sure your not using `JSON_FORCE_OBJECT`? – Lawrence Cherone Jan 02 '18 at 21:30
  • Pretty sure, the code you see above is only missing the php opening tag –  Jan 02 '18 at 21:32
  • Actually it's not a dup I fixed my problem there –  Jan 02 '18 at 21:33
  • @user2426691 When you say name of the object you mean key/index right e.g "0" and "1" etc? Then is the same issue as last time, which you say you fixed. Can you be a little more clear about what's wrong as to me its because of json-force-object, i've tested your code and it does not produce the same as your saying. https://3v4l.org/5hOQa – Lawrence Cherone Jan 02 '18 at 21:41
  • the indexed get added in array_filter I suppose (according to my tests). I'll write a short answer for a workaround – Jeff Jan 02 '18 at 21:41
  • wait, now that I'm reading @LawrenceCherone 's comment (and the working example) and answer in your prev question, it doesn't make sense anymore. – Jeff Jan 02 '18 at 21:46
  • Last question I wanted to put all my json objects in an array. This was accomplished and now works, but now the objects in the array have indexes as name –  Jan 02 '18 at 21:48

1 Answers1

2

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"]
astrangeloop
  • 1,430
  • 2
  • 13
  • 11