Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/267.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何通过在php数组中搜索特定值来获取密钥_Php_Arrays - Fatal编程技术网

如何通过在php数组中搜索特定值来获取密钥

如何通过在php数组中搜索特定值来获取密钥,php,arrays,Php,Arrays,我只想通过搜索id_img=17的值来获取密钥。 这是阵列: $array_test = array ( 0 => array("id_img" => 18, "desciption" => "Super image", "author" => "Person 1"), 1 => array("id_img" => 17, "desciption" => "Another image", "author" => "Person 2"), 2

我只想通过搜索id_img=17的值来获取密钥。 这是阵列:

$array_test = array (
0  => array("id_img" => 18, "desciption" => "Super image", "author" => "Person 1"),
1  => array("id_img" => 17, "desciption" => "Another image", "author" => "Person 2"),
2  => array("id_img" => 22, "desciption" => "The last image", "author" => "John Doe"),
);

谢谢你的帮助。

我喜欢做没有foreach或for循环的事情,如果可能的话,纯粹出于个人喜好

<?php
$found=false;
$searched=17;
foreach($array_test as $k=>$data)
 if($data['id_img']==$searched)
   $found=$key;
这是我的目标:

$array_test = array (
    0  => array("id_img" => 18, "desciption" => "Super image", "author" => "Person 1"),
    1  => array("id_img" => 17, "desciption" => "Another image", "author" => "Person 2"),
    2  => array("id_img" => 22, "desciption" => "The last image", "author" => "John Doe"),
);

$result = array_filter( $array_test, function( $value )
{
    return $value['id_img'] == 17 ? true : false;
});
$key = array_keys( $result )[0];

print_r( $key );

我不使用循环,而是使用
array\u filter()
仅获取数组中与我的规则匹配的项(如闭包的return语句中定义的)。因为我知道我只有一个值为17的ID,所以我知道我在
$result
数组中只会有一个项目。然后我从数组键中检索第一个元素(使用
array\u keys($result)[0]
)-这是在原始数组中保存id\u img=17的键。

因此,在本例中,您希望获得1键(id\u img=17)?在请求帮助时,同时,你应该解释一下为了解决你的问题你尝试了什么,以及它是如何不起作用的。否则会违反StackOverflow的基本规则,很可能会让您的问题被否决和/或关闭。好的,我尝试过:$position=array\u search($id\u img,$array\u test['id\u img');但它不起作用。欢迎来到StackOverflow。正如Jon所说,为了得到一个真正的问题,您应该解释(在问题中)您尝试了什么,或者至少是文档中您不理解的部分。社区会很乐意提供帮助,但不会根据规范提供免费代码。谢谢,它工作得很好,我认为可以使用数组搜索功能来避免循环。不客气。请记住,下一次这么做是为了编码帮助,而不是为您编码。另外,别忘了接受你问题的最佳答案。这是一种没有循环的有趣方式。这是可行的,但我找不到如何将值17作为变量传递
$id\u img
。我必须把它放在全球。不!请不要使用全局变量,除非它是整个应用程序所需要的!这是非常糟糕的做法。:)相反,您可以使用use关键字将变量导入到函数中:function($value)use($id\u img){function body…}。如果您对匿名函数感兴趣,我建议您阅读一下php关于匿名函数的文档。无论如何,从性能的角度来看,在您的案例中使用循环会更有效:一旦找到要查找的内容,您总是可以从循环中中断,但您不能从匿名函数中断。以下是匿名函数的文档:感谢链接,我将测试这种方法以了解匿名函数的更多信息,我不知道。关于我的$id\u img,我在代码中使用它,为什么在全局中使用它。我想把你想要的代码放在函数中,因为我在代码的不同部分使用它。无论如何,我停止了循环,当值被发现时,就像你建议的那样,所以我限制了在处理数百张图片时的功耗。
Try:

$array_test = array (
0  => array("id_img" => 18, "desciption" => "Super image", "author" => "Person 1"),
1  => array("id_img" => 17, "desciption" => "Another image", "author" => "Person 2"),
2  => array("id_img" => 22, "desciption" => "The last image", "author" => "John Doe"),
);

$result = null;

foreach($array_test as $key => $val )
{
  if( $val['id_img'] == 17 )
    $result = $key;

}
return $result;
$array_test = array (
    0  => array("id_img" => 18, "desciption" => "Super image", "author" => "Person 1"),
    1  => array("id_img" => 17, "desciption" => "Another image", "author" => "Person 2"),
    2  => array("id_img" => 22, "desciption" => "The last image", "author" => "John Doe"),
);

$result = array_filter( $array_test, function( $value )
{
    return $value['id_img'] == 17 ? true : false;
});
$key = array_keys( $result )[0];

print_r( $key );