Page 1 of 1

how to know the not empty indexes of an array ?

Posted: Tue Dec 23, 2008 4:45 am
by aneuryzma
My array lenght is 7.

However if I want to print the content at any index I get this error message:

Notice: Undefined offset: 0 (or 1, or 2, or 3..)

Is it maybe because it is declared, but not initialized ?

thanks

Re: how to know the not empty indexes of an array ?

Posted: Tue Dec 23, 2008 5:07 am
by requinix
Array keys don't have to be numbers, and they don't have to start at zero if they are.

Code: Select all

print_r(array(
    5 => 'five',
    'string' => "string for a key",
    -13 => "a negative number"
));
If you want to get rid of the keys and number everything starting at zero use array_values.

Re: how to know the not empty indexes of an array ?

Posted: Tue Dec 23, 2008 5:12 am
by mintedjo
If you don't need to use the numbers for anything then consider using a foreach loop

Code: Select all

foreach($array as $key => $val){
//Do yo thang
}

Re: how to know the not empty indexes of an array ?

Posted: Tue Dec 23, 2008 5:13 am
by aneuryzma
I don't know the keys. I just want to know if myArray[0] is empty or not. myArray[1].. is empty or not..
I need to know where the values are stored..

Re: how to know the not empty indexes of an array ?

Posted: Tue Dec 23, 2008 5:34 am
by requinix
empty? More like isset.

Re: how to know the not empty indexes of an array ?

Posted: Tue Dec 23, 2008 5:42 am
by Chris Corbyn
aneuryzma wrote:I don't know the keys. I just want to know if myArray[0] is empty or not. myArray[1].. is empty or not..
I need to know where the values are stored..
array_keys($yourArray)

Re: how to know the not empty indexes of an array ?

Posted: Tue Dec 23, 2008 10:05 am
by hitmanmx