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 a key exists in multi dimensional array with PHP

2 Answers

0 votes
$users = [
    [
        "id" => '1234567',
        "fname" => 'Tim',
    ],
    [
        "id" => '9876563',
        "fname" => 'Tom',
    ],
    [
        "id" => '3805732',
        "fname" => 'Mr. T',
    ],
    [
        "id" => '4563524',
        "fname" => 'John',
    ]
];
 
$key = array_search(4563524, array_column($users, 'id'));
 
echo $key; 
 
 
  
/*
run: 
 
3
   
*/  

 



answered Sep 11, 2017 by avibootz
edited Sep 15, 2017 by avibootz
0 votes
$users = [
    [
        "id" => '1234567',
        "fname" => 'Tim',
    ],
    [
        "id" => '9876563',
        "fname" => 'Ben',
    ],
    [
        "id" => '3805732',
        "fname" => 'Selena',
    ],
    [
        "id" => '4563524',
        "fname" => 'Fei',
    ]
];
 
$ids = array_column($users, 'id', 'id'); 
if (isset($ids[4563524]))
    echo "key exist"; 
 
 
  
/*
run: 
 
key exist
   
*/ 

 



answered Sep 12, 2017 by avibootz
edited Sep 15, 2017 by avibootz

Related questions

6 answers 487 views
1 answer 124 views
2 answers 417 views
417 views asked Jun 17, 2014 by avibootz
...