Jump to content

check for array index exsistance


calmchess

Recommended Posts

here is an easy one  i want to check an array to see if its index exists  if it does do something  else do something  else........but i get an error every time I try to create an if statement

 

 

 

if($arr0[1]==null){

//do nothin

}else{

echo "////////".$arr0[1];

}

Link to comment
Share on other sites

calmchess,

 

PHP has a built in function to do this: key_exists('key_to_find', $inThisArray)

 

See code below.

 

Scot L. Diddle, Richmond VA

 


<?php

Header("Cache-control: private, no-cache");
Header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
Header("Pragma: no-cache");


$someArray = array(1 => 'onesValue', 2 => 'twosValue', 4 => 'foursValue' );


// Echos Line 2
if(key_exists('3', $someArray)){
    echo "Line 1: Value for key = '3' exists and his value is " . $someArray3 . "<br /><br/> \n";
}
else{
echo "Line 2: Value for key = '3' does not exist" . "<br /><br/> \n";
}

// Echos Line 1
if(key_exists('4', $someArray)){
    echo "Line : Value for key = '4' exists and his value is '" . $someArray[4] . "'<br /><br/> \n";
}
else{
echo "Line 1: Value for key = '4' does not exist" . "<br /><br/> \n";
}

?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.