Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,895 questions

51,826 answers

573 users

How to check if key exists in array in PHP

2 Answers

0 votes
$arr = array('name' => "Avi", 'age' => 48, 'profession' => "Programmer");
if (array_key_exists('name', $arr)) 
    echo "The 'name' element exist";
 
/*
run:

The 'name' element exist

*/


answered Jun 17, 2014 by avibootz
edited Jul 4, 2015 by avibootz
0 votes
$arr = array('first_name' => "Avi", 
             'last_name' => "Bootz", 
             'age' => 48, 
             'profession' => "Programmer");
 
// check if many keys exists
function array_multi_key_exists($keys, $search_arr)
{
    $arr_keys = explode(':', $keys);
    foreach($arr_keys as $key)
        if(!array_key_exists($key, $search_arr))
            return false;
     
    return true;
}
 
if (array_multi_key_exists('first_name:last_name:profession', $arr))
    echo "exist"; // exist
else
    echo "NOT exit";
echo "<br>";
if (array_multi_key_exists('first_name:address', $arr))
    echo "exist";
else
    echo "NOT exit"; // NOT exit
 
/*
run:

exist
NOT exit

*/


answered Jun 17, 2014 by avibootz
edited Jul 4, 2015 by avibootz

Related questions

1 answer 124 views
2 answers 251 views
6 answers 487 views
2 answers 233 views
1 answer 171 views
1 answer 183 views
...