gpt4 book ai didi

php - 按值搜索时返回数组的索引

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:19:44 27 4
gpt4 key购买 nike

我有一个来自 MySQL 结果的数组。我希望能够按值搜索并获取数组中的索引位置。

我的数组 $array_result 如下所示

Array (
[0] => Array ( [scores] => 90 )
[1] => Array ( [scores] => 80 )
[2] => Array ( [scores] => 73 )
[3] => Array ( [scores] => 0 )
[4] => Array ( [scores] => 0 )
[5] => Array ( [scores] => 0 )
[6] => Array ( [scores] => 0 )
[7] => Array ( [scores] => 0 )
[8] => Array ( [scores] => 0 )
[9] => Array ( [scores] => 0 )
[10] => Array ( [scores] => 0 )
[11] => Array ( [scores] => 0 )
[12] => Array ( [scores] => 0 )
[13] => Array ( [scores] => 0 )
[14] => Array ( [scores] => 0 )
[15] => Array ( [scores] => 0 )
[16] => Array ( [scores] => 0 )
[17] => Array ( [scores] => 0 )
)

示例:我想搜索 80 并获取 1 的索引。我已经尝试过这个函数,但没有得到正确的响应:array_search(80, $array_result)

最佳答案

使用 foreach 循环来实现这一点。 Online link ,试试这个。您的第一个匹配项将返回索引。

数组

$array_result = array( 
array ("scores" => 90 ),
array ("scores" => 80 ),
array ("scores" => 73 ),
array ("scores" => 0 ),
array ("scores" => 0 ),
array ("scores" => 0 ),
array ("scores" => 0 ),
array ("scores" => 0 ),
array ("scores" => 0 ),
array ("scores" => 0 ),
array ("scores" => 0 ),
array ("scores" => 0 ),
array ("scores" => 0 ),
array ("scores" => 0 ),
array ("scores" => 0 ),
array ("scores" => 0 ),
array ("scores" => 0 ),
array ("scores" => 0 )
);

foreach($array_result as $key => $val){
if(in_array(80, $val)){
$out = $key;
break;
}
}

echo $out;

输出

1

关于php - 按值搜索时返回数组的索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36603241/

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