PHP array_search用于多维数组和返回键

时间:2017-03-30 14:19:44

标签: php arrays multidimensional-array

我试图在多维数组中搜索一个值(下面只是大数组的一部分)并获得该值的键,但我自己无法管理它。这是我尝试过的:

Array
(
    [0] => Array
        (
            [0] => SMEG - 30
            [1] => ALES
            [2] => -
            [3] => -
            [4] => -
            [5] => ALES
            [6] => 44-
            [7] => -
            [8] => FR*S30*E36*1*1
            [9] => FR*S30*E36*1*1
            [10] => US*S30
            [11] => Oui
            [12] => 3376
            [13] => Normale
            [14] => -
        )

    [1] => Array  // <-- wanted key
        (
            [0] => SMEG - 30
            [1] => ALES
            [2] => -
            [3] => Chemin Des Sports
            [4] => -
            [5] => ALES
            [6] => -
            [7] => -
            [8] => FR*S30*E37*2*1  // <-- wanted value
            [9] => FR*S30*E37*2*1
            [10] => FR*S30
            [11] => Oui
            [12] => 33762
            [13] => Normale
            [14] => -
        )

    [2] => Array
        (
            [0] => SMEG - 30
            [1] => ALES
            [2] => 0
            [3] => Ecole Des Mines
            [4] => -
            [5] => ALES
            [6] => 4-
            [7] => -
            [8] => FR*S30*E38*2*1
            [9] => FR*S30*E38*2*1
            [10] => FR*S30
            [11] => Oui
            [12] => 3376
            [13] => Normale
            [14] => -
        )
)

$key = array_search("FR*S30*E37*2*1", array_column($data, '8'));
var_dump($data[$key]);

使用此代码,我无法获得所需数组的密钥。我做错了什么?

3 个答案:

答案 0 :(得分:1)

如果您不需要密钥,可以使用array_filter

$result = array_filter($data, function($item) use ($search) {
    return $item[8] == $search;
})[0];

如果您需要密钥,可以像这样修改

$key = false;
$result = array_filter($data, function($item, $k) use ($search, &$key) {
    if ($item[8] == $search) {
        $key = $k;
        return true;
    }
    return false;
}, ARRAY_FILTER_USE_BOTH)[0];

要处理未找到结果的案例,您必须跳过[0]方并测试count($result) != 0

答案 1 :(得分:0)

尝试使用此函数进行递归搜索:

function array_search_recursive($needle, array $haystack)
    {
        foreach ($haystack as $key => $value) {
            $current_key = $key;

            if ($needle === $value or (is_array($value) && array_search_recursive($needle, $value) !== false)) {
                return $current_key;
            }
        }
        return false;
    }

$key = array_search_recursive("FR*S30*E37*2*1", $data);

答案 2 :(得分:0)

PHP code demo

<?php
$array=Array
(
    0=>   Array
        (
            0=>   "SMEG - 30",
            1=>   "ALES",
            2=>   "-",
            3=>   "-",
            4=>   "-",
            5=>   "ALES",
            6=>   "44-",
            7=>   "-",
            8=>   "FR*S30*E36*1*1",
            9=>   "FR*S30*E36*1*1",
            10=>   "US*S30",
            11=>   "Oui",
            12=>   "3376",
            13=>   "Normale",
            14=>   "-"
        ),

    1=>   Array
        (
            0=>   "SMEG - 30",
            1=>   "ALES",
            2=>   "-",
            3=>   "Chemin Des Sports",
            4=>   "-",
            5=>   "ALES",
            6=>   "-",
            7=>   "-",
            8=>   "FR*S30*E37*2*1",
            9=>   "FR*S30*E37*2*1",
            10=>   "FR*S30",
            11=>   "Oui",
            12=>   "33762",
            13=>   "Normale",
            14=>   "-",
        ),

    2=>   Array
        (
            0=>   "SMEG - 30",
            1=>   "ALES",
            2=>   "0",
            3=>   "Ecole Des Mines",
            4=>   "-",
            5=>   "ALES",
            6=>   "4-",
            7=>   "-",
            8=>   "FR*S30*E38*2*1",
            9=>   "FR*S30*E38*2*1",
            10=>   "FR*S30",
            11=>   "Oui",
            12=>   "3376",
            13=>   "Normale",
            14=>   "-",
        )
);
$requiredKey=null;
$requiredValue=null;
finder($array,"FR*S30*E37*2*1");
function finder($array,$search)
{
    global $requiredKey,$requiredValue;
    foreach($array as $key => $value)
    {
        if(in_array($search, $value))
        {
            $requiredKey=$key;
            $requiredValue=$search;
            break;
        }
    }
}
echo $requiredKey;
echo $requiredValue;