I can't wrap my head around this one.
When I create an array it begins with an empty value. The values that needs to be in the array starts as second.
Array:
Array (
[0] =>
[1] => Value 1
[2] => Value 2
[3] => Value 3
)
Code:
$categories = array();
$query2 = mysql_query("SELECT * FROM books_categories");
do{
array_push($categories, $category['description']);
}while($category=mysql_fetch_assoc($query2));
How do I accomplish to get the first item in the array to be Value 1?
If the array hasn't been created yet and you try to print the value of the variable, it will print null (meaning it doesn't reference any object yet).
An array value can be non-empty, empty (cardinality zero), or null. The individual elements in the array can be null or not null. An empty array, an array value of null, and an array for which all elements are the null value are different from each other. An uninitialized array is a null array.
The length property sets or returns the number of elements in an array. By knowing the number of elements in the array, you can tell if it is empty or not. An empty array will have 0 elements inside of it.
An object array is populated with null values when it's instantiated. Collections on the other hand are empty at the beginning, so there' nothing that can be "populated" in the first place - well, you could fill them with null values if you wanted, but what would be the point of that?
It is because of do while loop try while loop
while($category=mysql_fetch_assoc($query2)){
array_push($categories, $category['description']);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With