Reputation: 33
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);
}
}
Upvotes: 0
Views: 1455
Reputation: 21979
This is caused by you giving $activeCourses
the value of array_push()
. array_push()
return an int value. To fix this, just update this line to not returning value, since array_push()
's parameter is passed by reference:
$activeCourses = array_push($activeCourses, $course);
Changed it to:
array_push($activeCourses, $course);
Upvotes: 1
Reputation: 6233
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;
}
}
Upvotes: 1
Reputation: 677
You can use the []
more easier:
$activeCourses = array();
foreach ($allCourses as $course) {
if (strtotime($course->end_date) > time()) {
$activeCourses[] = $course;
}
}
Upvotes: 0