PHP: Creating list of unique values using nested foreach loops

Pretty sure my code is close to working, but I am having some difficulty. I’m using WordPress, which has some functions in the first foreach loop to grab the pages that the current user authored, but I know that part works. My focus is on the second loop, which is intended to make an array comprised of unique values (page categories).

Have had trouble responding to users directly on here before (computer too old to update browser) so thanks ahead of time for the help!

$args= array('sort_column' => 'post_date', 'sort_order' => 'desc', 'authors' => $current_user -> user_login;
    $pages = get_pages($args);
$uniques = array();
    foreach ($pages as $page) { 
$categories = get_the_category($page->ID);
    foreach ($uniques as $unique) {
        if ( in_array($categories[0]->name, $unique) )
        {
        continue;
        }
array_push($unique, $categories[0]->name);
echo end($unique[0]);
echo '<br>';
echo $categories[0]->name;
    }
}

Related posts

3 comments

  1. You have a problem with your foreach ($uniques as $unique) { line. That loop will never execute since $uniques is always empty.

  2. Resolved, thanks for your help!

    $uniques = array ();
    
    foreach ($pages as $page) {
    $categories = get_the_category ($page->ID);
    
    foreach ($categories as $category) {
        if ( ! in_array ($uniques, $category->name) ) {
            $uniques [] = $category->name;
        }
    }
    
    foreach ($uniques as $unique) {
        if (in_array ($categories [0]->name, $unique)) {
            continue;
        }
    }
    

    }

  3. I am getting correct that you wish to get all the unique values of categories in the array $uniques and $categories[0]->name is fetching the category name for you.

    Then try using the array_unique() function of php then you might use array_values() to sort the indexes.

    <?php 
    $args= array('sort_column' => 'post_date', 'sort_order' => 'desc', 'authors' => $current_user -> user_login;
    $pages = get_pages($args);
    $uniques = array();
    foreach ($pages as $page) { 
        $categories = get_the_category($page->ID);
        $uniques[]  = $categories[0]->name;
    }
    
    $uniques_arr = array_unique($uniques);
    print_r($uniques_arr);
    $uniques_sorted = array_values($uniques);
    print_r($uniques_sorted);
    ?>
    

    This will get you only the first category from all the pages as we are only inserting 0 index. To get all the categories of all pages use below code

    <?php 
    $args= array('sort_column' => 'post_date', 'sort_order' => 'desc', 'authors' => $current_user -> user_login;
    $pages = get_pages($args);
    $uniques = array();
    foreach ($pages as $page) { 
        $categories = get_the_category($page->ID);
        foreach ($categories as $category) {
            $uniques[]  = $category->name;
        }
    }
    
    $uniques_arr = array_unique($uniques);
    print_r($uniques_arr);
    $uniques_sorted = array_values($uniques);
    print_r($uniques_sorted);
    ?>
    

Comments are closed.