Home > OS >  remove duplicates from array of objects array
remove duplicates from array of objects array

Time:02-20

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

CodePudding user response:

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

  •  Tags:  
  • php
  • Related