calmchess Posted May 27, 2010 Share Posted May 27, 2010 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 https://forums.phpfreaks.com/topic/203071-check-for-array-index-exsistance/ Share on other sites More sharing options...
ScotDiddle Posted May 27, 2010 Share Posted May 27, 2010 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 https://forums.phpfreaks.com/topic/203071-check-for-array-index-exsistance/#findComment-1064061 Share on other sites More sharing options...
calmchess Posted May 27, 2010 Author Share Posted May 27, 2010 array_key_exists() Link to comment https://forums.phpfreaks.com/topic/203071-check-for-array-index-exsistance/#findComment-1064063 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.