Billy Bones
Billy Bones

Reputation: 2975

PHP Array Unintentionally Turning into Int

I might be searching incorrectly but I cannot find a solution to my issue. I have an empty array created for values in a database. Each row should push a value to the array but after the first element my array becomes an integer.

if ($num_rows > 0){
    $dates = [];
    while($row = $result->fetch_assoc()){   
        var_dump($dates);
        $dates = array_push($dates, $row['date']);
    }
}

Warning: array_push() expects parameter 1 to be array, integer given

EDIT the date field of my db is a varchar

Upvotes: 0

Views: 57

Answers (1)

u_mulder
u_mulder

Reputation: 54831

Just:

while($row = $result->fetch_assoc()){   
    var_dump($dates);
    array_push($dates, $row['date']);
}

without assigning result. Because array_push works with array by reference. And returns not new array but:

Returns the new number of elements in the array.

Upvotes: 4

Related Questions