Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merge demonym into demonyms #369

Merged
merged 2 commits into from
Jan 3, 2020
Merged

Conversation

alexsegura
Copy link
Contributor

Fixes #368

@mledoze
Copy link
Owner

mledoze commented Dec 20, 2019

Thank you for this. Do you mind putting eng before fra to respect the alphabetic order?

@alexsegura
Copy link
Contributor Author

@mledoze here you go

At first I thought it wouldn't be possible as object properties would get serialized in an unpredictable order, but it's as simple as setting the object keys in the order you expect.

💙PHP 💙

<?php

$countries = json_decode(file_get_contents('./countries.json'));

foreach ($countries as $key => $country) {
    if (isset($country->demonyms) && isset($country->demonym)) {

        $demonyms_eng = new \stdClass();
        $demonyms_eng->f = $country->demonym;
        $demonyms_eng->m = $country->demonym;

        $demonyms = $country->demonyms;

        unset($country->demonym);
        unset($country->demonyms);

        $country->demonyms = new \stdClass();

        $countries[$key]->demonyms->eng = $demonyms_eng;
        $countries[$key]->demonyms->fra = $demonyms->fra;
    }
}

file_put_contents('./countries.json', json_encode($countries, JSON_PRETTY_PRINT));

@mledoze
Copy link
Owner

mledoze commented Dec 21, 2019

Or you could just use ksort for a simpler and more generic solution; here is a tested example:

$countries = json_decode(file_get_contents('countries.json'), true);

$countries = array_map(static function ($country) {
    if (isset($country['demonyms'], $country['demonym'])) {
        $country['demonyms']['eng'] = [
            'f' => $country['demonym'],
            'm' => $country['demonym'],
        ];
        ksort($country['demonyms']);
        unset($country['demonym']);
    }
    return $country;
}, $countries);

file_put_contents('countries.json', json_encode($countries, JSON_PRETTY_PRINT));

@mledoze
Copy link
Owner

mledoze commented Dec 21, 2019

Could you also remove the description of the demonym property in the README?

@alexsegura
Copy link
Contributor Author

Done.

@mledoze mledoze merged commit 3fd2765 into mledoze:master Jan 3, 2020
@mledoze
Copy link
Owner

mledoze commented Jan 3, 2020

It's merged, thank you for your help on this. I will draft a new release now.

@alexsegura alexsegura deleted the demonyms-merge branch January 4, 2020 12:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Merge demonym into demonyms
2 participants