Jump to content

check for array index exsistance


calmchess

Recommended Posts

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";
}

?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.