php 循环遍历数组,并检查其中一个值是否是字符串的一部分[duplicate]

z9ju0rcb  于 2023-01-16  发布在  PHP
关注(0)|答案(3)|浏览(136)
    • 此问题在此处已有答案**:

Filter multidimensional array based on partial match of search value(3个答案)
How to use dd() without stopping the program on laravel(3个答案)
Laravel dd function prevents blade including the master template(2个答案)
How does actullay dd() works in laravel(1个答案)
Dump within map function in Laravel(1个答案)
4天前关闭。
∮我有∮
a字符串$input ="约翰的一加二",以及
一个数组$devices

array:54 [
  0 => array:1 [
    "name" => "xperia-z-ultra"
  ]
  1 => array:1 [
    "name" => "xbox-one"
  ]
  2 => array:1 [
    "name" => "xbox-360"
  ]
  3 => array:1 [
    "name" => "withings-camera"
  ]
  4 => array:1 [
    "name" => "withing"
  ]
  5 => array:1 [
    "name" => "win-7"
  ]
  6 => array:1 [
    "name" => "surface-pro-4"
  ]
  7 => array:1 [
    "name" => "surface-book"
  ]
  8 => array:1 [
    "name" => "surface-3"
  ]
  9 => array:1 [
    "name" => "roku"
  ]
  10 => array:1 [
    "name" => "play-station-4"
  ]
  11 => array:1 [
    "name" => "one-plus-2"
  ]
  12 => array:1 [
    "name" => "nokia-lumia"
  ]
  13 => array:1 [
    "name" => "no-img"
  ]
  14 => array:1 [
    "name" => "nexus-6p"
  ]
  15 => array:1 [
    "name" => "nest"
  ]
  16 => array:1 [
    "name" => "moto-x"
  ]
  17 => array:1 [
    "name" => "macbook-pro"
  ]
  18 => array:1 [
    "name" => "mac-pro"
  ]
  19 => array:1 [
    "name" => "mac-book"
  ]
  20 => array:1 [
    "name" => "mac-book-air"
  ]
  21 => array:1 [
    "name" => "lg-g4"
  ]
  22 => array:1 [
    "name" => "laptop"
  ]
  23 => array:1 [
    "name" => "iphone-6"
  ]
  24 => array:1 [
    "name" => "iphone-6-s"
  ]
  25 => array:1 [
    "name" => "iphone-6-s-plus"
  ]
  26 => array:1 [
    "name" => "iphone-6-plus"
  ]
  27 => array:1 [
    "name" => "iphone-5s"
  ]
  28 => array:1 [
    "name" => "iphone-5"
  ]
  29 => array:1 [
    "name" => "iphone-4"
  ]
  30 => array:1 [
    "name" => "ipad"
  ]
  31 => array:1 [
    "name" => "ipad-pro"
  ]
  32 => array:1 [
    "name" => "hue"
  ]
  33 => array:1 [
    "name" => "htc-one-m9"
  ]
  34 => array:1 [
    "name" => "hp-laptop"
  ]
  35 => array:1 [
    "name" => "galaxy-s6"
  ]
  36 => array:1 [
    "name" => "galaxy-s6-edge"
  ]
  37 => array:1 [
    "name" => "galaxy-s5"
  ]
  38 => array:1 [
    "name" => "galaxy-note-5"
  ]
  39 => array:1 [
    "name" => "galaxy-note-4"
  ]
  40 => array:1 [
    "name" => "galaxy-note-3"
  ]
  41 => array:1 [
    "name" => "galaxy-note-2"
  ]
  42 => array:1 [
    "name" => "chromecast"
  ]
  43 => array:1 [
    "name" => "chrome-book"
  ]
  44 => array:1 [
    "name" => "blackberry-z30"
  ]
  45 => array:1 [
    "name" => "blackberry-z10"
  ]
  46 => array:1 [
    "name" => "blackberry-prev"
  ]
  47 => array:1 [
    "name" => "blackberry-bold"
  ]
  48 => array:1 [
    "name" => "belkin-wemo"
  ]
  49 => array:1 [
    "name" => "august"
  ]
  50 => array:1 [
    "name" => "apple-watch"
  ]
  51 => array:1 [
    "name" => "apple-tv"
  ]
  52 => array:1 [
    "name" => "androidtablet"
  ]
  53 => array:1 [
    "name" => "amazon-tv"
  ]
]

∮我想要∮
循环遍历数组,并检查$input子字符串是否包含$devices数组的设备名称之一。
∮我试过∮

foreach ($devices as $i=>$device) {
    if( stripos($input, $device['name']) !== false){
        dd('True');
    }
    else
        dd('False');

}

∮我得到了
即使如此,我知道one-plus-2实际上存在于我的设备数组中。

11 => array:1 ["name" => "one-plus-2"]

我错过什么了吗?

pcww981p

pcww981p1#

您可以将array_filter()strpos()一起使用:

$matches = array_filter($devices, function($device) use ($input) {
  return strpos($input, $device['name']) !== FALSE;
});
dd($matches);

array_filter()不会重新索引,因此第一个键不是0,而是11(在本例中)。

e7arh2l6

e7arh2l62#

你把针和干草堆参数对调了
试试这个

if( stripos($device['name'], $input) !== false){ ...}
omvjsjqw

omvjsjqw3#

条纹〉〉〉条纹
“stripos”错了正确的是“strpos”
并且参数必须是
strpos(要搜索的字符串,需要的字符串)

if (strpos($device['name'], $input) !== false) {
    echo 'true';
}

相关问题