Search code examples
phparraysarray-push

array_push() expects parameter 1 to be array


I have a PHP problem.

I have this code block

$arr_foundits = array();
foreach($its as $it){
    //print_r($it);
    $post_categories = wp_get_post_categories( $it->ID );
    $cats = array();

    foreach($post_categories as $c){
        $cat = get_category( $c );
        $catname = strtolower($cat->name);
            //print_r($catname);
            if($catname=='uncategorized'){
                continue;
            }

            $squery = get_search_query();


            if(strpos($catname, strtolower($squery))!==false){
                //echo 'ceva';
                $found = true;
                $arr_foundits = array_push($arr_foundits, $it->ID);//line 80 hier
                printf('<li><h4><a href="%1$s">%2$s</a></h4><p>%3$s</p></li>', get_permalink($it->ID), $it->post_title, get_the_excerpt_by_id($it->ID));
            }
    }
}

The problem I am having is with the $arr_foundits array, I always receive this error, and clearly it's in array, no way an integer because I declare it there and nowhere else.

Any solution to this error ?

a
(source: imgbin.org)


Solution

  • array_push modifies the original array, it does not return the "new" one. It returns the new length of the array, which is an integer. so the second time the loop comes around you are feeding it a number instead of an array. Docs