Skip to content
Advertisement

array_push() expects parameter 1 to be array, int given

I am getting array_push() expect parameter one to be an array, any solution?

$activeCourses = array();
foreach ($allCourses as $course) {
   if (strtotime($course->end_date) > time()) {
       $activeCourses = array_push($activeCourses, $course);
   }
}

Advertisement

Answer

you have referenced the variable as an array first time. but when you are using it for array push with $activeCourses = it becomes an integer field as array_push returns an integer value and then when it comes to next array push in the next iteration, your activeCourses variable is no longer an array. so use like

$activeCourses = array();
foreach ($allCourses as $course) {
   if (strtotime($course->end_date) > time()) {
       array_push($activeCourses, $course);
   }
}

or

$activeCourses = array();
foreach ($allCourses as $course) {
   if (strtotime($course->end_date) > time()) {
       $activeCourses[] = $course;
   }
}
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement