Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does my array start with empty value?

Tags:

arrays

php

mysql

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?

like image 913
MrSlippyFist Avatar asked Dec 19 '12 12:12

MrSlippyFist


People also ask

Why is my array printing out null?

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).

Can an array have empty values?

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.

What does it mean for an array to be empty?

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.

Is array filled with null?

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?


1 Answers

It is because of do while loop try while loop

while($category=mysql_fetch_assoc($query2)){
    array_push($categories, $category['description']);
}
like image 148
senK Avatar answered Nov 01 '22 01:11

senK