略微加速

PHP官方手册 - 互联网笔记

PHP - Manual: array_key_exists

2024-11-14

array_key_exists

(PHP 4 >= 4.0.7, PHP 5, PHP 7, PHP 8)

array_key_exists检查数组里是否有指定的键名或索引

说明

array_key_exists(string|int $key, array $array): bool

数组里有键 key 时,array_key_exists() 返回 truekey 可以是任何能作为数组索引的值。

参数

key

要检查的键。

array

一个数组,包含待检查的键。

返回值

成功时返回 true, 或者在失败时返回 false

注意:

array_key_exists() 仅仅搜索第一维的键。 多维数组里嵌套的键不会被搜索到。

范例

示例 #1 array_key_exists() 例子

<?php
$search_array 
= array('first' => 1'second' => 4);
if (
array_key_exists('first'$search_array)) {
    echo 
"The 'first' element is in the array";
}
?>

示例 #2 array_key_exists()isset() 的对比

isset() 对于数组中为 null 的值不会返回 true,而 array_key_exists() 会。

<?php
$search_array 
= array('first' => null'second' => 4);

// 返回 false
isset($search_array['first']);

// 返回 true
array_key_exists('first'$search_array);
?>

注释

注意:

由于为了兼容以前版本,如果 object 当做 array 传入 array_key_exists(),同时 key 是对象的属性,也会返回 true。 此行为在 PHP 7.4.0 弃用且在 PHP 8.0.0 移除。

要检查对象是否有某个属性,应该使用 property_exists()

参见

add a noteadd a note

User Contributed Notes 39 notes

up
226
Nik Tang
10 years ago
If you want to take the performance advantage of isset() while keeping the NULL element correctly detected, use this:

if (isset(..) || array_key_exists(...))
{
...
}

Benchmark (100000 runs):
array_key_exists() : 205 ms
is_set() : 35ms
isset() || array_key_exists() : 48ms

Note:
The code for this check is very fast, so you shouldn't warp the code into a single function like below, because the overhead of calling a function dominates the overall performance.

function array_check(...)
{
    return (isset(..) || array_key_exists(...))
}
up
11
php at vxvr dot de
1 year ago
While Nik Tang's remark about the performance increase when using isset() first for PHP <= 7.3 is true, it is no longer valid for 7.4 and 8.0.
array_key_exists() is now as fast as isset() and the use of both is now slower for cases where the key is zero or actually does not exist.
up
4
Md. Abutaleb
2 years ago
Extending from here : https://www.php.net/manual/en/function.array-key-exists.php#90687

<?php

# Working for null check
# Warning for boolean check
# Warning for float check

$a = array(null => 1, false => 2, true => 3, 4.6 => 4, "08" => 5, "8" => 6);

var_dump(array_key_exists(null, $a)); // true
var_dump(array_key_exists(false, $a)); // warning and false
var_dump(array_key_exists(true, $a)); // warning and false
var_dump(array_key_exists(4.6, $a)); // warning and false
(array_key_exists("08", $a)); // true
var_dump(array_key_exists(8, $a)); // true

?>
up
22
Matej
3 years ago
Beware that if the array passed to array_key_exists is NULL, the return value will also be NULL.

This is undocumented behaviour, moreover the documentation (and return typehint) suggest that the array_key_exists function only returns boolean value. But that's not the case.
up
41
mankyd at gmail dot com
14 years ago
You'll notice several notes on this page stating that isset() is significantly faster than array_key_exists(). This may be true except for one small hitch. isset() will return false for arrays keys that have there value set to NULL, which is therefore not entirely accurate.

Example:

<?php
$foo
= array();
$foo['bar'] = NULL;

var_dump(isset($foo['bar']));
var_dump(array_key_exists('bar', $foo));
?>

will output:
bool(false)
bool(true)

Be aware of this!
up
4
Ellert van Koperen
4 years ago
Rudi's multidimensional array_key_exists function was not working for me, so i built one that is.
Enjoy.

function array_multi_key_exists( $arrNeedles, $arrHaystack){
    $Needle = array_shift($arrNeedles);
    if(count($arrNeedles) == 0){
        return(array_key_exists($Needle, $arrHaystack));
    }else{
        if(!array_key_exists($Needle, $arrHaystack)){
            return false;
        }else{
            return array_multi_key_exists($arrNeedles, $arrHaystack[$Needle]);
        }
    }
}
up
18
webmaster at oehoeboeroe dot nl
13 years ago
The way array_key_exists handles null, float, boolean, and 'integer-representing string' keys is inconsistent in itself and, in the case of bool and float, with the way these are converted when used as array offset.

<?php
$array
= array(null => 1, false => 2, true => 3, 4.6 => 4, "08" => 5, "8" => 6);
var_export($array);

echo
"\nnull is " . (array_key_exists(null, $array) ? '' : 'not ') . "a key.\n";
echo
'false is ' . (array_key_exists(false, $array) ? '' : 'not ') . "a key.\n";
echo
'true is ' . (array_key_exists(true, $array) ? '' : 'not ') . "a key.\n";
echo
'4.6 is ' . (array_key_exists(4.6, $array) ? '' : 'not ') . "a key.\n";
echo
'"08" is ' . (array_key_exists("08", $array) ? '' : 'not ') . "a key.\n";
echo
'"8" is ' . (array_key_exists("8", $array) ? '' : 'not ') . "a key.\n";
?>

Output:

array (
  '' => 1,
  0 => 2,
  1 => 3,
  4 => 4,
  '08' => 5,
  8 => 6,
)
null is a key.
false is not a key.
true is not a key.
4.6 is not a key.
"08" is a key.
"8" is a key.

Well, and you get this warning three times (on the bools and the float, but not on the null):

Warning:  array_key_exists() [function.array-key-exists]: The first argument should be either a string or an integer in /var/www/php/test.php on line 6
up
15
PHPEric
12 years ago
Very simple case-insensitive array_key_exists:

bool (in_array(strtolower($needle), array_map('strtolower', array_keys($haystack))))
up
8
Rudi
10 years ago
I've got a new take on the multi key function I would like to share.

<?php
function array_multi_key_exists(array $arrNeedles, array $arrHaystack, $blnMatchAll=true){
   
$blnFound = array_key_exists(array_shift($arrNeedles), $arrHaystack);
   
    if(
$blnFound && (count($arrNeedles) == 0 || !$blnMatchAll))
        return
true;
   
    if(!
$blnFound && count($arrNeedles) == 0 || $blnMatchAll)
        return
false;
   
    return
array_multi_key_exists($arrNeedles, $arrHaystack, $blnMatchAll);
}
?>

Hope you'll find it usefull.
up
2
alarido at almadialetica dot com dot br
1 year ago
/// it works like array_key_exists that can go deeper

function in_array_deeply($keys, $multi)
{
    $depths = array();
    $fun = null;
    // inner, recursive function
    function in_fun($level, $arr_keys, $subject, &$depths) {
        // only if array
        if (is_array($subject)) {
            // if key exists
            if (array_key_exists($arr_keys[$level], $subject)) {
                // record depth and go deeper
                $depths[] = $arr_keys[$level];
                in_fun($level+1, $arr_keys, $subject[$arr_keys[$level]], $depths);
            }
        }
    };
    //
    in_fun(0, $keys, $multi, $depths);
    $srchd = implode('.',$keys);
    $found = implode('.',$depths);
    //
    return ($srchd==$found);
}

/// example usage

$mapa = array('quadrada','comprida','amarela');

$cidade = array(
    'redonda' => array(
        'curta' => 'o seu filme'
    ),
    'quadrada' => array(
        'longa' => array(
            'azul' => array('logo',2,'mais',2,'são',4),
            'amarela' => array('então',3,'vezes',2,'são',6),
            'verde' => array('senão',100,'dividido por',2,'é',50)
        ),
        'extravagante' => array(
            'vermelha' => 'chama atenção',
            'vinho' => 'cor de uva',
            'ocre' => 1255
        ),
        'comprida' => array(
            'amarela' => 'brasilia dos mamonas',
            'branca' => 'bandeira da paz',
            'preta e branca' => 'peças do xadrez'
        )
    ),
    'oval' => array(
        'conde' => 'lobo'
    ),
    'plana' => array(
        'curta' => array(
            'azul' => array('e',2,'mais',2,'são',4),
            'amarela' => array('sim',3,'vezes',2,'são',6),
            'verde' => array('verdade',100,'dividido por',2,'é',50)
        )
    )
);

$encontrou = in_array_deeply($mapa, $cidade);

echo $encontrou;

/// if the tree you search for exists, it will print out 'true'
up
8
Venkata Subbaraju
6 years ago
array_key_exists perfect for $_GET .

As there will be browser level restriction on length of URL , number of keys in $_GET global variable will be very limited . So array_key_exists will not cause any performance overhead in this context . isset function can be conveniently ignored while handling $_GET variable.

But for $_POST , the max array size has to be configure in server PHP configuration . So cannot rely on array_key_exists for $_POST if max array size is not optimized .

Here is an example with array_key_exists switching between content-types :

if(array_key_exists('format',$_GET))
{
              if($_GET['format']=="json")
              {
                      header('Content-Type: application/json');
              }
              else{
                     //handle any other format whitelisted for the application
              }
}
else {
               error_log("format parameter missing . using default html format");
               header('Content-Type: text/html');
}
up
6
mc dot watras at gmail dot com
7 years ago
array_key_exists doesn't work with objects implementing ArrayAccess interface. It also ignores possible __get() method in such objects, despite the fact it accepts object as a second parameter. It works only with 'real' properties.

<?php
class A implements ArrayAccess {
    public
$data;
    public function
offsetExists($offset) {
        return isset(
$this->data[$offset]);
    }
    public function
__get($property) {
        return
$this->data[$property];
    }
}

$a = new A();
$a->data['somekey'] = 1;
var_dump(array_key_exists('somekey', $a)); // returns false
var_dump(array_key_exists('data', $a));  // returns true
?>
up
4
tech at signhere envy dot de
11 years ago
Hey, this function is able to rename a key inside an array.

If the key to be replaced doesn't exist inside the array, or the new key already exists in the array, the function will return FALSE.
Otherwise, the array with the renamed key will be returned.

Hope this will be useful for someone.
<?php
function array_rename_key($array, $key, $newkey){
    if( !
array_key_exists($key, $array) || array_key_exists($newkey, $array)) return FALSE;
   
$uid = uniqid(''); //To clearly identify the Element.
   
$preserve_value = $array[$key]; //Keep the Value
   
$array[$key] = $uid; //Overwrite Value with ID
   
$array = array_flip($array); //Flip the Array keys and values
   
$array[$uid] = $newkey; //Set Value of the ID with new Key.
   
$array = array_flip($array); //Everything back in Place.
   
$array[$newkey] = $preserve_value;
    return
$array;
}
?>
up
9
Lucknut dot xbl at googlemail dot com
14 years ago
I found this function very good to use if your want your urls like index.php?login or index.php?register
e.g.
<?php
if( array_key_exists( 'home',$_GET ) ) {
    echo
"Home - its where the heart is.";
} else if(
array_key_exists( 'login',$_GET ) ) {
    echo
"Login code here!";
} else if(
array_key_exists( 'register',$_GET ) ) {
    echo
"Register code here!";
} else {
    echo
"Home - its where the heart is.";
}
?>
up
10
manhon824 at gmail dot com
11 years ago
I took hours for me to debug, and I finally recognized that,

You have to reset the $array before using array_key_exists
reset($array);
array_key_exists($needle,$array);

Or you will get no reply.
up
1
pierreantoine dot guillaume at gmail dot com
4 years ago
if you ever face a case in which your array_key_exists returns null, that might be because the second parameter you gave it is not an array.

For instance :

    var_dump(array_key_exists($my_key,$my_array));
    //bool(true);

    var_dump(array_key_exists($my_array, $my_key));
    //NULL
up
9
tmont
14 years ago
The argument of array_key_exists() vs. isset() came up in the workplace today, so I conducted a little benchmark to see which is faster:

<?php
   
// one-dimensional arrays
   
$array = array_fill(0,50000,'tommy is the best!');
   
$arraykeyexists_result = array();

   
$start = microtime(true);
    for (
$i = 0; $i < 100000; $i++) {
        if (
array_key_exists($i,$array)) {
           
$arraykeyexists_result[] = 1;
        }
        else {
           
$arraykeyexists_result[] = 0;
        }
    }
   
$arrtime = round(microtime(true)-$start,3);
   
   
$start = microtime(true);
    for (
$i = 0; $i < 100000; $i++) {
        if (isset(
$array[$i])) {
           
$arraykeyexists_result[] = 1;
        }
        else {
           
$arraykeyexists_result[] = 0;
        }
    }
   
$istime = round(microtime(true)-$start,3);
   
   
$totaltime = $arrtime+$istime;
   
$arrpercentage = round(100*$arrtime/$totaltime,3);
   
$ispercentage = round(100*$istime/$totaltime,3);   
   
    echo
"array_key_exists(): $arrtime [$arrpercentage%] seconds\n";
    echo
"isset():            $istime [$ispercentage%] seconds\n";

?>

On Windows, the output is similar to

array_key_exists(): 0.504 [82.895%] seconds
isset():            0.104 [17.105%] seconds

On Mac or Linux, isset() is faster but only by a factor of approximately 1.5.
up
2
falundir at gmail dot com
4 years ago
This function will return NULL when second argument is not an array. So it will bite you when using it like that:

<?php
$not_an_array
= null;
if (
array_key_exists($key, $not_an_array) === false) {
   
// the array doesn't have the key
   
return;
}

// happily proceed - surely the array does have the key (BUT IT DOESN'T!)
?>

That's because the condition null === false fails.

Instead you should use:

<?php
if (array_key_exists($key, $not_an_array) == false) {
    return;
}

// or

if (!array_key_exists($key, $not_an_array)) {
    return;
}

// or

if (is_array($not_an_array) === false || array_key_exists($key, $not_an_array) === false) {
    return;
}
?>
up
7
John
13 years ago
Here is a little function for case sensitivity to elaborate on what was said by MarkL from ##php (Freenode) and mmanning at mdanderson dot org from this page:

<?php
// Case sensitive version of array_key_exists() using preg_match()
function array_ikey_exists($key,$arr)
{
    if(
preg_match("/".$key."/i", join(",", array_keys($arr))))               
        return
true;
    else
        return
false;
}
?>

Not that anyone else couldn't have written this, but a concept like this strengthens reusability.  :)

Also, I've been running into issues with escaping for Regex, so I decided to give something like this a shot:

<?php
function array_ikey_exists($key,$arr)
{
   
$e = 0; //$key = addslashes($key);
   
if(is_array($arr) && $arr !==array())
    {
        foreach(
$arr as $k => $v)
        {   
            if(
strtolower($k) == strtolower($key))
               
$e++;
        }
        if(
$e>0)
            return
true;       
        else
            return
false;
    }
    else
        return
false;
}
?>

You could addslashes() to escape; it's just another approach.
up
2
david at madole dot net
14 years ago
Regarding performance differences between isset() and array_key_exists(), the differences may be there, but the function are not always interchangable.

Note that when $a[1] = null then isset($a[1]) == false but array_key_exists(1, $a) == true
up
4
Anonymous
11 years ago
I just want to note that array_key_exists() can be extremely slow for large (>200 keys) arrays.  Use isset($array($key)) instead!  My program ran in 3 minutes instead of 2 hours after switching to isset()!
up
2
glitch dot mr at gmail dot com
10 years ago
As you might know, isset() is actually working like @$variable===NULL. As the result, it doesn't actually catch variables set to NULL. If you want to check if variable is set (even to NULL), you can use array_key_exists on $GLOBALS, like there.

<?php
   $me
= null;
  
$se = 1;
   unset(
$se);
// $he is not set

if(array_key_exists('me', $GLOBALS)) echo "\$me exists\n";
if(
array_key_exists('se', $GLOBALS)) echo "\$se exists\n";
if(
array_key_exists('he', $GLOBALS)) echo "\$he exists\n";

?>

In this case, only $me will be detected as $se was removed when code was running and $he was never set.
up
2
moandsimon at btinternet dot com
12 years ago
This uses array_key_exists.

You have a multidimensional array of the form:

$rowsoriginal[] = array('field_wrkvolmin_value' => 216, 'field_wrkvolmax_value' => 1000);
$rowsoriginal[] = array('field_wrkvolmin_value' => 27, 'field_wrkvolmax_value' => 216);

Using print_r this will look like:

Array ( [0] => Array ( [field_wrkvolmin_value] => 216 [field_wrkvolmax_value] => 1000 ) [1] => Array ( [field_wrkvolmin_value] => 27 [field_wrkvolmax_value] => 216 ) )

This can be used to create a table by iterating over the rows that looks like this:

field_wrkvolmin_value          field_wrkvolmax_value
           216                                      1000
            27                                       216

when $rowsoriginal contain a fixed but unknown amount of values.

If you want to process this in an automatic way without knowing the keys etc, into a multidimensional array of the form:

$rowstemp = array('field_wrkvolmin_value' => array(216, 27), 'field_wrkvolmax_value' => array(1000, 216));

Using print_r this will look like:

Array ( [field_wrkvolmin_value] => Array ( [0] => 216 [1] => 27 ) [field_wrkvolmax_value] => Array ( [0] => 1000 [1] => 216 ) )

This can be used to iterate over the rows of a table to create a table in the form of:

field_wrkvolmin_value            216            27
field_wrkvolmax_value           1000           216

To do this you can use the following looping and conditional structure, using array_key_exists():

<?php
$rowstemp
= array();
foreach (
$rowsoriginal as $row) {
    foreach (
$row as $key => $value) {
        if (
array_key_exists($key, $rowstemp)) {
           
$rowstemp[$key][] = $value;
        }
        else {
           
$valuestemp = array($value);
           
$rowstemp[$key] = $valuestemp;
        }
    }
}
?>
up
1
Karim Ratib
13 years ago
Here's a function to return a reference to the first array element that has a given key. The code works for multidimensional arrays:

<?php
function &array_find_element_by_key($key, &$form) {
  if (
array_key_exists($key, $form)) {
   
$ret =& $form[$key];
    return
$ret;
  }
  foreach (
$form as $k => $v) {
    if (
is_array($v)) {
     
$ret =& array_find_element_by_key($key, $form[$k]);
      if (
$ret) {
        return
$ret;
      }
    }
  }
  return
FALSE;
}
?>
up
1
jens dot hoevenaars at gmail dot com
11 years ago
I created this function that uses array key exist to compare a form and a table to see if something has changed.

This can be very helpfull if you need to update a table record from a form but you do not want to display all table fields.

<?php
function($data_from_db, $form_data) {
$data = $data_from_db;
$keys = array_keys($data);

    for(
$i = 0; $i < count($data); $i++) {
        if(!
array_key_exists($keys[$i], $form_data)) {
           
$dbobject->$keys[$i] = $data[$keys[$i]];
        } else {
           
$dbobject->$keys[$i] = $form_data[$keys[$i]];
        }
    }
    return
$dbobject;
}
?>

you can then use the dbobject to update the table.
up
1
imaginary at friend dot com
10 years ago
An even simpler case-insensitive alternative to array_key_exists():

<?php

array_key_exists
(strtolower($key), array_change_key_case($search));
up
1
ellert at vankoperen dot nl
7 years ago
While working with large JSON datastructures I encountered an increasing need to check the presence of certain data all over the place.
This led to the function below that lets you simply access the data, and returns it if available, null if not.
Note that it uses a variable number of parameters, you can go as deep into a nested structure as you want.

<?php
function safe_array_access($ar){
   
$numargs = func_num_args();
   
$arg_list = func_get_args();
   
$aritterator = $ar;
    for(
$i = 1; $i < $numargs; $i++){
        if (isset(
$aritterator[$arg_list[$i]]) || array_key_exists($arg_list[$i], $aritterator)){
           
$aritterator = $aritterator[$arg_list[$i]];
        }else{
            return(
false);
        }
    }
    return(
$aritterator);
}
?>
Usage:
Instead of  $a['b']['c'] use safe_array_access($a, 'b', 'c');
up
2
johannes dot kingma at gmail dot com
9 months ago
I often need a value from an array and if missing, some default. So this became a staple in every utils.php file I include in my projects:

<?php
/**
* get array value if key exist otherwise default
* @param string $key needle
* @param mixed[] $array haystack
* @param mixed $default value if needle not found
*/
function array_value($key, $array, $default = '')
{
    if (
array_key_exists($key, $array))
        return
$array[$key];
    return
$default;
}
?>

Works great for $_POST and $_GET or for some $option array that I send to function of class constructor.
up
1
inker2576 at yahoo dot com
15 years ago
Further research on this has turned up that the performance problems are a known, confirmed bug in PHP 5.1.x, and have been fixed in PHP builds after September 2006.  You can find the bug report here:  http://bugs.php.net/bug.php?id=38812

However, just because it's a fixed bug doesn't really change the conclusion.  If you're writing a script and there's any chance it could be used on a PHP 5.1.x server, you should still avoid this function and use isset() or some other kind of test if you want it to run efficiently.
up
1
gmdebby at gmail dot com
12 years ago
A little function which take an array as keys

<?php
//note the s in the function name (keys)
function array_keys_exists($array,$keys) {
    foreach(
$keys as $k) {
        if(!isset(
$array[$k])) {
        return
false;
        }
    }
    return
true;
}
?>
//useful to validate a form for example
<form>
    <input type="text" name="field1" /><br />
    <input type="text" name="field2" /><br />
    <input type="text" name="field3" /><br />
    <input type="text" name="field4" /><br />
    <input type="text" name="field5" /><br />
</form>
<?php
if(!array_keys_exists($_POST,
array(
"field1","field2","field3","field4","field5")
)) {
   
//some fields are missing, dont do anything (maybe hacking)
} else {
   
//code ...
}
?>
up
1
jacobsingh at gmail dot com
14 years ago
I saw some examples above for array_keys_exist() or functions to see if multiple keys exist in a given array and return false if any of them don't.

Here is a simpler way to do this:

<?php

function array_keys_exist($keys,$array) {
    if (
count (array_intersect($keys,array_keys($array))) == count($keys)) {
        return
true;
    }
}

$array = array ('filename' => 'myfile', 'filesize' => 1234, 'filepath' => '/tmp/myfile');
$keys = array('filename','filesize','filepath');

echo
array_keys_exist($keys,$array);
//returns true

$keys[] = "somethingelse";

echo
array_keys_exist($keys,$array);
//Returns false

?>
up
1
wolf550e at gmail dot com
14 years ago
array_key_exists(), at least in 5.2.4, passes the array by value. I conclude this from seeing performance worsen as the array to search got bigger. isset() doesn't have this problem.
up
1
diogoshaw at gmail dot com
14 years ago
this function very good to use if you need to verify many variables:

<?php
function array_key_exists_r($keys, $search_r) {
   
$keys_r = split('\|',$keys);
    foreach(
$keys_r as $key)
    if(!
array_key_exists($key,$search_r))
    return
false;
    return
true;
}
?>

e.g.

<?php
if(array_key_exists_r('login|user|passwd',$_GET)) {
// login
} else {
// other
}
?>

works for me, enjoy.
dg shaw.
up
0
venimus at gmail dot com
4 years ago
Be warned that before checking the key existence with isset() it will be typecasted to integer if it is not a string or integer!

See Example #2 in http://php.net/manual/en/language.types.array.php

I.e.

$a = ['test']; // 'test' will have key 0
$key = false;

var_dump(isset($a[$key]) ); // true
up
0
Sandro Alves Peres
9 years ago
<?php
/*
    It also works on objects
    ***************************************
*/

class PHP
{

    private
$compiler;
    protected
$architecture;
    public
$version;
    public
$system = "Linux";
   
}

$php = new PHP();

$a = array_key_exists('compiler', $php);     # false
$b = array_key_exists('architecture', $php); # false
$c = array_key_exists('version', $php);      # true
$d = array_key_exists('system', $php);       # true

var_dump( $a, $b, $c, $d );

?>
up
0
Matt
15 years ago
mikael dot knutsson at gmail dot com:
I don't think it does, at least in PHP5?

For example, this outputs bool(false):

$ar = array ( 'outter' => array ( 'inner' => 1 ) );
var_dump(array_key_exists('inner', $ar));

So it doesn't actually check the inner array for the key 'inner'.
up
-2
p dot allgeier at web dot de
6 years ago
If u want to check if a key exists either in the array or in any subarray try the following:

<?php

function requireKey($needle, array $array)
    {
        foreach (
$array as $key => $value) {
            if (
$key === $needle) return $value;
            if (
is_array($value)) {
                if (
$x = $this->requireKey($key, $value)) return $x;
            }
        }
        return
false;
    }

?>

(( return $value for recursive array search, return true for recursive array key exists ))
up
-2
tom at edgedesigns dot org
12 years ago
The multi_array_key_exists() function posted by alishahnovin at hotmail dot com [which has since been removed] does not always return the expected result.

This modified version does.

<?php
/**
* multi_array_key_exists function.
*
* @param mixed $needle The key you want to check for
* @param mixed $haystack The array you want to search
* @return bool
*/
function multi_array_key_exists( $needle, $haystack ) {

    foreach (
$haystack as $key => $value ) :

        if (
$needle == $key )
            return
true;
       
        if (
is_array( $value ) ) :
             if (
multi_array_key_exists( $needle, $value ) == true )
                return
true;
             else
                 continue;
        endif;
       
    endforeach;
   
    return
false;
}

?>
up
-4
zildo
5 years ago
This function will look for a key in multidimensional arrays, and return the paths to all the results found, or return false if none have been found.

<?php

function array_multi_key_exists($key, $array, $first=true, &$return=array()){

if(
$first){

while(
$ret = array_multi_key_exists($key, &$array, false)){
$r[]=$ret;
}

if(empty(
$r[0])){
return
$r[0]=array();
}else{
return
$r;
}

}

foreach(
$array as $k => $v){

if(
$k === $key && !(is_array($v))){

unset(
$array[$k]);
array_unshift($return, $k);
return
$return;

}elseif(
$k === $key && is_array($v)){

if(empty(
$v)){
unset(
$array[$k]);
}else{

if(
array_multi_key_exists($key, &$array[$k], false, $return)){

array_unshift($return, $k);
return
$return;

}

}

array_unshift($return, $k);
unset(
$array[$k]);
return
$return;

}else{

if(
is_array($v)){

if(
array_multi_key_exists($key, &$array[$k], false, $return)){

array_unshift($return, $k);
return
$return;

}

}

}

}

return
false;

}

?>

The next function takes the array of paths returned by array_multi_key_exists as parameter and returns the corresponding values.

<?php

function array_multi_values($paths, $array){

$return = array();

foreach(
$paths as $n => $path){
$val = $array;
foreach(
$path as $dim => $key){

$val = $val[$key];

}

$return[$n] = $val;
unset(
$val);

}

return
$return;

}

?>

This next function takes the paths array returned by array_multi_key_exists, and unsets the corresponding values of the original array, returning the clipped array.

<?php

function array_multi_clip($paths, $array, $key=0, $path=false){

while(
$path==false){

foreach(
$paths as $path){
array_multi_clip($paths, &$array, 0, $path);
}
return
$array;
}

$n = count($path);

if(
$key == $n-1){
unset(
$array[$path[$key]]);
return
$array;
}else{

array_multi_clip($paths, &$array[$path[$key]], ++$key, $path);
}

}

?>

Examples:

<?php

$array
= array(

'colours' => array(

'red' => array(

'veg' => 'tomato',
'fruit' => 'strawberry',
'green' => 'colourblind',

),

'green' => array(

'green' => 'envy',
'unripe' => 'veggies',

),

'blue' => 'smurfs',

),

);

$key = 'green';

$paths = array_multi_key_exists($key, $array);
print_r($paths);

?>

The result will be an array of paths:

Array
(
    [0] => Array
        (
            [0] => colours
            [1] => red
            [2] => green
        )

    [1] => Array
        (
            [0] => colours
            [1] => green
            [2] => green
        )

    [2] => Array
        (
            [0] => colours
            [1] => green
        )

)

As it can be seen, if one of the results is an array which again contains the key searched, both will show up in the results.

Example of array_multi_values:

<?php

$values
= array_multi_values($paths, $array);
print_r($values);

?>

Result:

<?php

Array
(
    [
0] => colourblind
   
[1] => envy
   
[2] => Array
        (
            [
green] => envy
           
[unripe] => veggies
       
)

)

?>

Example of array_multi_clip():

<?php

$clipped
= array_multi_clip($paths, $array);
print_r($clipped);

?>

Result:

<?php
Array
(
    [
colours] => Array
        (
            [
red] => Array
                (
                    [
veg] => tomato
                   
[fruit] => strawberry
               
)

            [
blue] => smurfs
       
)

)

?>

官方地址:https://www.php.net/manual/en/function.array-key-exists.php

北京半月雨文化科技有限公司.版权所有 京ICP备12026184号-3