PHP: Creating list of unique values using nested foreach loops
-
Pretty sure my code is close to working, but I am having some difficulty. The first foreach loop is 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). I think that $uniques is always empty, but I have no clue how to fix it.
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; } }
Viewing 5 replies - 1 through 5 (of 5 total)
Viewing 5 replies - 1 through 5 (of 5 total)
- The topic ‘PHP: Creating list of unique values using nested foreach loops’ is closed to new replies.