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

发布于 2024-11-25 15:10:45 字数 1071 浏览 1 评论 0原文

我已经在网站和网络上进行了搜索,并尝试了各种递归函数等,但都无济于事,所以我希望这里有人能指出我哪里出错了:)

我有一个名为 $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 
    ) 
)

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

我想知道 $meetID 中的值是否出现在数组中的 [Meet_Name] 中,并简单地评估这个 true 或 false。

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

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;

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.

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

Any help very much appreciated before I shoot myself :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

寄居人 2024-12-02 15:10:45
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')) {
    //...
}

我不确定你的意思

$meetID 出现在 [Meet_Name] 中

,但只需将 $h[$key]==$needle 条件替换为满足您需求的内容即可。

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]

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

苯莒 2024-12-02 15:10:45

对于一维数组,您可以使用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);
}

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

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);
}
假扮的天使 2024-12-02 15:10:45

编写一个类似这样的方法:

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]);

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文