gpt4 book ai didi

php - 我如何在数组中搜索值?

转载 作者:行者123 更新时间:2023-12-03 23:06:12 25 4
gpt4 key购买 nike

我有一个这样的数组,我正在尝试搜索一个值使用这个数组:

$arr = Array('Slovenly Mexico','Slovenly Europe','Greece');

我正在使用这种条件。请帮忙

if($a[storename] == $arr[$i]){
//some code

}

但它不起作用,因为它作为索引值进行搜索,因此会出错。

Array
(
[0] => Array
(
[storename] => Greece
)

[1] => Array
(
[storename] => Slovenly Europe
)


[3] => Array
(
[storename] => Slovenly Europe
)

[4] => Array
(
[storename] => Greece
)

[5] => Array
(
[storename] => Slovenly Mexico

)

[6] => Array
(
[storename] => Slovenly Europe
)

[7] => Array
(
[storename] => Slovenly Mexico

)
}

最佳答案

看看这个函数:

http://php.net/manual/en/function.array-search.php

<?php
$array = array(0 => 'blue', 1 => 'red', 2 => 'green', 3 => 'red');

$key = array_search('green', $array); // $key = 2;
$key = array_search('red', $array); // $key = 1;
?>

关于php - 我如何在数组中搜索值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10428031/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com