Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

remove duplicates from array of objects array

how do I remove duplicate from below JSON
checking website_name and only keeping num_followers with greatest value per date

the output should be just one value of website name in each websites array for each date

[
    {
        "date": "2022-02-15",
        "websites": [
            {
                "website_name": "instagram",
                "num_followers": "123146780"
            },
            {
                "website_name": "instagram",
                "num_followers": "123134954"
            },
            {
                "website_name": "tiktok",
                "num_followers": "123184229"
            }
        ]
    },
    {
        "date": "2022-02-14",
        "websites": [
            {
                "website_name": "instagram",
                "num_followers": "123057832"
            },
            {
                "website_name": "tiktok",
                "num_followers": "123058141"
            },
            {
                "website_name": "tiktok",
                "num_followers": "123058219"
            },
            {
                "website_name": "instagram",
                "num_followers": "123059280"
            }
        ]
    }
]

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

This can be one with some foreach loops and if-statements.

I’ve explained each step in comments:

// New array to store the new data in
$data = [];

foreach (json_decode($input, true) as $day) {
    // Initialize array to store the data for the current date in
    $items = ['date' => $day['date']];

    // We'll store the sites with most followers here
    $sites = [];
    
    foreach ($day['websites'] as $site) {
        $name = $site['website_name'];
        
        // If the site hasn't been added yet, add it and jump to next
        if (key_exists($name, $sites) === false) {
            // Use the site name as key so we easily can replace it
            $sites[$name] = $site;
            continue;
        }
        
        // Only replace it if this has more followers than the stored one
        if ($sites[$name]['num_followers'] < $site['num_followers']) {
            $sites[$name] = $site;
        }
    }
    
    // Add the sites to the date array. Use array_values so we
    // get them without the site name as they key
    $items['websites'] = array_values($sites);
    
    // Add the filtered out list to the main array
    $data[] = $items;
}

Result:

[
    {
        "date": "2022-02-15",
        "websites": [
            {
                "website_name": "instagram",
                "num_followers": "123146780"
            },
            {
                "website_name": "tiktok",
                "num_followers": "123184229"
            }
        ]
    },
    {
        "date": "2022-02-14",
        "websites": [
            {
                "website_name": "instagram",
                "num_followers": "123059280"
            },
            {
                "website_name": "tiktok",
                "num_followers": "123058219"
            }
        ]
    }
]

Here’s a demo: https://3v4l.org/RZU5s

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading