• Resolved jetfighter

    (@jetfighter)


    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)
  • You’ve got your values mixed up. You don’t want the second foreach() looping through the $uniques values as they aren’t set, so no loops will be done.

    If I’m right, you’d want something like this?

    $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) {
    		if ( ! in_array ($uniques, $category->name) ) {
    			$uniques [] = $category->name;
    		}
    	}
    
        foreach ($uniques as $unique) {
            if (in_array ($categories [0]->name, $unique)) {
    			continue;
            }
        }
    }
    
    echo "<pre>";
    var_export ($uniques);
    echo "</pre>";
    Thread Starter jetfighter

    (@jetfighter)

    Thanks for the reply!! Looks like a smart approach. So there were some errors:
    Says that the ‘$category->name’ portion of line:
    if ( ! in_array ($uniques, $category->name) ) {
    as well as the ‘$unique’ portion of line:
    if (in_array ($categories [0]->name, $unique)) {
    are strings when they should be arrays? So I changed $unique to $uniques in the second line, and that got rid of that error. Now trying to figure out the other error, from that first line.

    Any ideas?

    I think I had the arguements the wrong way around…

    if ( ! in_array ($category->name, $uniques) ) {

    I can never remember the order of arguments for some functions off the top of my head! That’s what I use an IDE for…

    Thread Starter jetfighter

    (@jetfighter)

    It works! Thank you so much! I would have never figured that out on my own. Just needed to switch out that first line in my response above with this:
    if ( ! in_array ($category->name, $uniques) ) {

    Works beautifully. I don’t understand how it works really, but very thankful.
    Guess I need to spend some time wrapping my head around loops some more.

    Have a good week

    Thread Starter jetfighter

    (@jetfighter)

    Resolved.

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.