PHP在多维/嵌套数组中查找值

时间:2023-01-16 21:31:05

I've trawled the site and the net and have tried various recursive functions etc to no avail, so I'm hoping someone here can point out where I'm going wrong :)

我已经对网站和网络进行了拖网,并尝试了各种递归功能等无济于事,所以我希望有人在这里可以指出我哪里出错了:)

I have an array named $meetingArray with the following values;

我有一个名为$ meetingArray的数组,其中包含以下值;

Array ( 
    [0] => Array ( 
        [Meet_ID] => 9313 
        [Meet_Name] => 456136 
        [Meet_CallInNumber] => 
        [Meet_AttendeeCode] => 
        [Meet_Password] => 
        [Meet_ScheduledDateTime] => 2011-07-18 16:00:00 
        [Meet_ModeratorCode] => 
        [Meet_RequireRegistration] => 0 
        [Meet_CurrentUsers] => 0 
    ) 
    [1] => Array ( 
        [Meet_ID] => 9314 
        [Meet_Name] => 456120 
        [Meet_CallInNumber] => 
        [Meet_AttendeeCode] => 
        [Meet_Password] => 
        [Meet_ScheduledDateTime] => 2011-07-18 16:00:00 
        [Meet_ModeratorCode] => 
        [Meet_RequireRegistration] => 0 
        [Meet_CurrentUsers] => 0 
    ) 
)

I also have a variable named $meetID.

我还有一个名为$ meetID的变量。

I want to know if the value in $meetID appears in [Meet_Name] within the array and simply evaluate this true or false.

我想知道$ meetID中的值是否出现在数组中的[Meet_Name]中,并且只是评估此true或false。

Any help very much appreciated before I shoot myself :)

在我开枪之前,非常感谢任何帮助:)

3 个解决方案

#1


4  

function multi_in_array($needle, $haystack, $key) {
    foreach ($haystack as $h) {
        if (array_key_exists($key, $h) && $h[$key]==$needle) {
            return true;
        }
    }
    return false;
}

if (multi_in_array($meetID, $meetingArray, 'Meet_Name')) {
    //...
}

I am unsure what you mean by

我不确定你的意思

$meetID appears in [Meet_Name]

$ meetID出现在[Meet_Name]中

but simply substitute the $h[$key]==$needle condition with something that meets your needs.

但只需用满足您需求的东西替换$ h [$ key] == $ needle条件。

#2


0  

For single-dimensional arrays you can use array_search(). This can be adapted for multi-dimensional arrays like so:

对于单维数组,您可以使用array_search()。这可以适用于多维数组,如下所示:

function array_search_recursive($needle, $haystack, $strict=false, $stack=array()) {
    $results = array();
    foreach($haystack as $key=>$value) {
        if(($strict && $needle === $value) || (!$strict && $needle == $value)) {
            $results[] = array_merge($stack, array($key));
        }

        if(is_array($value) && count($value) != 0) {
            $results = array_merge($results, array_search_recursive($needle, $value, $strict, array_merge($stack, array($key))));
        }
    }
    return($results);
}

#3


0  

Write a method something like this:

写一个像这样的方法:

function valInArr($array, $field, $value) {
 foreach ($array as $id => $nestedArray) {
  if (strpos($value,$nestedArray[$field])) return $id;
  //if ($nestedArray[$field] === $value) return $id; // use this line if you want the values to be identical
 }
 return false;
}

$meetID = 1234;
$x = valInArr($array, "Meet_Name", $meetID);
if ($x) print_r($array[$x]);

This function will evaluate true if the record is found in the array and also enable you to quickly access the specific nested array matching that ID.

如果在数组中找到记录,则此函数将评估为true,并且还使您能够快速访问与该ID匹配的特定嵌套数组。

#1


4  

function multi_in_array($needle, $haystack, $key) {
    foreach ($haystack as $h) {
        if (array_key_exists($key, $h) && $h[$key]==$needle) {
            return true;
        }
    }
    return false;
}

if (multi_in_array($meetID, $meetingArray, 'Meet_Name')) {
    //...
}

I am unsure what you mean by

我不确定你的意思

$meetID appears in [Meet_Name]

$ meetID出现在[Meet_Name]中

but simply substitute the $h[$key]==$needle condition with something that meets your needs.

但只需用满足您需求的东西替换$ h [$ key] == $ needle条件。

#2


0  

For single-dimensional arrays you can use array_search(). This can be adapted for multi-dimensional arrays like so:

对于单维数组,您可以使用array_search()。这可以适用于多维数组,如下所示:

function array_search_recursive($needle, $haystack, $strict=false, $stack=array()) {
    $results = array();
    foreach($haystack as $key=>$value) {
        if(($strict && $needle === $value) || (!$strict && $needle == $value)) {
            $results[] = array_merge($stack, array($key));
        }

        if(is_array($value) && count($value) != 0) {
            $results = array_merge($results, array_search_recursive($needle, $value, $strict, array_merge($stack, array($key))));
        }
    }
    return($results);
}

#3


0  

Write a method something like this:

写一个像这样的方法:

function valInArr($array, $field, $value) {
 foreach ($array as $id => $nestedArray) {
  if (strpos($value,$nestedArray[$field])) return $id;
  //if ($nestedArray[$field] === $value) return $id; // use this line if you want the values to be identical
 }
 return false;
}

$meetID = 1234;
$x = valInArr($array, "Meet_Name", $meetID);
if ($x) print_r($array[$x]);

This function will evaluate true if the record is found in the array and also enable you to quickly access the specific nested array matching that ID.

如果在数组中找到记录,则此函数将评估为true,并且还使您能够快速访问与该ID匹配的特定嵌套数组。