how to know the not empty indexes of an array ?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
aneuryzma
Forum Contributor
Posts: 106
Joined: Sat May 17, 2008 7:03 am

how to know the not empty indexes of an array ?

Post 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
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

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

Post 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.
mintedjo
Forum Contributor
Posts: 153
Joined: Wed Nov 19, 2008 6:23 am

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

Post 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
}
aneuryzma
Forum Contributor
Posts: 106
Joined: Sat May 17, 2008 7:03 am

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

Post 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..
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

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

Post by requinix »

empty? More like isset.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

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

Post 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)
hitmanmx
Forum Newbie
Posts: 13
Joined: Mon Dec 22, 2008 1:29 am

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

Post by hitmanmx »

Post Reply