簡體   English   中英

搜索數組內部包含多個數組,怎么辦?

[英]Search inside array contain multiple arrays, how?

我做錯了什么? 此代碼始終不返回任何內容。 如何搜索多個數組?

<?php

    $a = array("x", "c");
    $b  = array("v", "b");
    $c = array("n", "m");

    $arrayglobal  = array($a, $b, $c);

    if (array_walk_recursive($arrayglobal,"b")) 
         {
         print  "found";
         }

    else {
         print  "nothing";
         }

?>

如果只是二維數組,則可以循環然后檢查in_array()

$search = 'b';
$result = 'nothing';

foreach ($arrayglobal as $array) {
    if (in_array($search, $array)) {
        $result = 'found';
        break;
    }
}

print $result;

歡迎來到堆棧溢出和PHP世界。 在我們的php世界中,我們有相同的規則。 規則如下:

  1. 變量以$符號開頭,后跟變量名稱

  2. 變量名稱必須以字母或下划線字符開頭

  3. 變量名稱不能以數字開頭

  4. 變量名稱只能包含字母數字字符和下划線(Az,0-9和_)

  5. 變量名稱區分大小寫($ var和$ VAR是兩個不同的變量)

這個規則對我們的世界非常嚴格.. xD

$a = array("x", "c");// A variable name must start with a letter or the underscore character
$b  = array("v", "b");
$c = array("n", "m");

$f=1;
$arrayglobal  = array($a, $b, $c);

foreach ($arrayglobal as $array) {
if (in_array("b", $array)) {
    $f=0;
     echo "found";
    break;
 }
}
if($f == 1){
         echo "nothing";

}

array_walk_recursive()與目標數組一起用作第一個參數,將匿名函數用作第二個參數,該匿名函數將處理該匿名函數中的每個數組。

您可以這樣使用它:

<?php

$_1 = array("x", "c");
$_2  = array("v", "b");
$_3 = array("n", "m");

$arrayglobal  = array($_1, $_2, $_3);


array_walk_recursive($arrayglobal, function ($item)
{
    echo $item == "b" ? "found\n" : "nothing\n";
});

?>

使用array_merge()合並數組,然后使用下面的代碼。

<?php 
    $a = array("x", "c");
    $b = array("v", "b");
    $c = array("n", "m");

    $arrayglobal  = array_merge($a, $b, $c);

    if(in_array("b",$arrayglobal)){
        print  "found";
    }
    else{
        print  "nothing";
    }
?>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM