Php 按值和返回键搜索数组

Php 按值和返回键搜索数组,php,arrays,search,key,Php,Arrays,Search,Key,我从$\u GET变量中传递了c和3的值,我希望在数组中查找这些值并检索它们的键。如何在数组中搜索以返回准确的键 下面的代码 <?php $array1 = array(0 => 'a', 1 => 'c', 2 => 'c'); $array2 = array(0 => '3', 1 => '2', 2 => '3'); $key1 = array_search('c', $array1); $key2 = array_search('3', $a

我从
$\u GET
变量中传递了c和3的值,我希望在数组中查找这些值并检索它们的键。如何在数组中搜索以返回准确的键

下面的代码

<?php

$array1 = array(0 => 'a', 1 => 'c', 2 => 'c');
$array2 = array(0 => '3', 1 => '2', 2 => '3');

$key1 = array_search('c', $array1);
$key2 = array_search('3', $array2);

?>
虽然我在期待

$key1 = 2;
$key2 = 2;

函数返回的结果与它应该返回的完全相同。值“c”第一次出现在$array1的索引1处,值“3”第一次出现在$array2的索引0处

此行为记录在中,如果您不喜欢,它甚至为您提供了一个替代方案:

如果在干草堆中多次发现针,则第一个匹配的键 他回来了。要返回所有匹配值的键,请使用 改为使用可选的搜索值参数数组_keys()


如果要查找具有该值的最后一个键,可以先反转数组

$array1 = array(0 => 'a', 1 => 'c', 2 => 'c');
$array2 = array(0 => '3', 1 => '2', 2 => '3');

$key1 = array_search('c', $array1);
$key2 = array_search('3', $array2);

var_dump($key1,$key2); //output: int(1) int(0)


$key1 = array_search('c', array_reverse($array1, true));
$key2 = array_search('3', array_reverse($array2, true));

var_dump($key1,$key2); //output: int(2) int(2)

我敢肯定,不管你想做什么,都有一种更明智的方法。

也许类似于:

<?php
// for specificly 2 arrays
function search_matching($match1, $match2, array $array1, array $array2) {
    foreach($array1 as $key1 => $value1) {
        // we may want to add $strict = false argument to distinguish between == and === match
        // see http://php.net/manual/en/function.array-search.php
        if($value1 == $match1 and isset($array2[$key1]) and $array2[$key1] == $match2) {
            return $key1;
        }
    }

    return null;
}


// unlimited
function search_matching(array($matches), array $array/*, ...*/) {
    if( count($matches) != func_num_args() - 1)
         throw new \Exception("Number of values to match must be the same as the number of supplied arrays");

    $arrays = func_get_args();
    array_shift($arrays);  // remove $matches
    $array = array_unshift($arrays); // array to be iterated

    foreach($array as $key => $value) {
         if($value == $matches[0]) {
               $matches = true;

               foreach($arrays as $keyA => $valueA) {
                    if(! isset($arrays[$key] or $valueA != $matches[$keyA+1]) {
                        $matches = false;
                        break;
                    }                  
               }

               if($matches)
                   return $key;
         }
    }

    return null;
}

这是一个准确的结果,因为这些键确实具有您要搜索的值。您的标准是要查找具有该值的最后一个键吗?有没有更好的方法来处理这个问题?有什么不准确的地方吗?我希望$array1和$array2中的键是匹配在一起的,因为这两个数组都链接到执行sql过程。如果传递值为“c”和“3”,则必须匹配,两个返回的键必须为$key1=2和$key2=2。没有找到最后一个键,而是找到从以前的输入传递的值键,这些值可以是“c”和“2”,其中这些键返回正确,因为键是第一个匹配的。
foreach ($array1 as $key => $value) {
    if ($value == 'c' && $array2[$key] == '3') {
        echo "The key you are looking for is $key";
        break;
    }
}
<?php
// for specificly 2 arrays
function search_matching($match1, $match2, array $array1, array $array2) {
    foreach($array1 as $key1 => $value1) {
        // we may want to add $strict = false argument to distinguish between == and === match
        // see http://php.net/manual/en/function.array-search.php
        if($value1 == $match1 and isset($array2[$key1]) and $array2[$key1] == $match2) {
            return $key1;
        }
    }

    return null;
}


// unlimited
function search_matching(array($matches), array $array/*, ...*/) {
    if( count($matches) != func_num_args() - 1)
         throw new \Exception("Number of values to match must be the same as the number of supplied arrays");

    $arrays = func_get_args();
    array_shift($arrays);  // remove $matches
    $array = array_unshift($arrays); // array to be iterated

    foreach($array as $key => $value) {
         if($value == $matches[0]) {
               $matches = true;

               foreach($arrays as $keyA => $valueA) {
                    if(! isset($arrays[$key] or $valueA != $matches[$keyA+1]) {
                        $matches = false;
                        break;
                    }                  
               }

               if($matches)
                   return $key;
         }
    }

    return null;
}