简体   繁体   English

通过作为变量给出的多个键进行多维数组搜索?

[英]Multidimensional Array Search by Multiple Keys given as Variable?

I have a multidimensional array which has keys and key has values or have another array with keys and values so I want to search by keys but in input like 230 is user input and it will go to 3 then 4 then 1 if result is a value but not an array it must print the value like input = 230 result should be = "3-4-1" so I need to str_split the number and search it 1 by 1 if first number is array then look for second kinda edit1 = I found the way to split the key我有一个多维数组,它有键,键有值,或者有另一个数组有键和值,所以我想按键搜索,但在输入中,比如 230 是用户输入,如果结果是值,它将 go 到 3 然后 4 然后 1但不是数组,它必须打印像 input = 230 结果应该是 = "3-4-1" 这样的值,所以我需要 str_split 数字,如果第一个数字是数组,则按 1 逐个搜索,然后寻找第二种 edit1 = I找到了拆分密钥的方法

//edit1
    $keys = "021";
       $keysSplit =str_split($keys, strlen($keys)/strlen($keys));
       echo $keys[0];
//edit 1 ends
    $arr = [0 => [0=>"1-1", 1 => "1-2" , 2=>"1-3", 3=>[0=>"1-4-1", 1 => "1-4-2" , 2=>"1-4-3"]],
        1 => [0=>"2-1", 1 => "2-2" , 2=>"2-3"],
        2 => [0=>"3-1", 1 => "3-2" , 2=>"3-3", 3=>[0 =>"3-4-1" , 1=> "3-4-2"]],
];

$keys = "021";
function searchByKey($array , $keys){
    $result = [];
$keys = "021";
$keys =str_split($keys, strlen($keys)/strlen($keys));
$key1 = $keys[0];
$key2 = $keys [1];
$key3 = $keys [2];
    foreach ($array as $key1 => $value){
        if (is_array($value)){
            $key1 = null;
          $key1 = $key2;
          $key2 = $key3;
        return searchByKey($value , $key1);    
        }
        else {
           $result=$value;
           echo $result;
        }
    }
}
$arr = searchByKey($arr, $keys);

The function only works as key and value given and it will print every key and value on the key it asked first so its not the thing I wanted to do could anyone help and explain? function 只能作为给定的键和值工作,它会在它首先询问的键上打印每个键和值,所以这不是我想做的事情,有人可以帮忙解释一下吗? Answer given by @Anggara I made it in to function; @Anggara 给出的答案我进入了 function;

$input = "11";


function searchByNumber($array, $input){
$result = $array;    
for ($i = 0; $i < strlen($input); $i++) {
    if (is_array($result)) {
        $result = $result[$input[$i]];
    } else {
        $result = "Does not exists";
        break;
    }
}
echo $result;
}


$arr = searchByNumber($arr, $input);

You can access char in string like accessing an array.您可以像访问数组一样访问字符串中的字符。 For example:例如:

$input = "230";
// $input[0] is "2"
// $input[1] is "3"
// $input[2] is "0"

So my approach is to loop for each character in input key, and look for corresponding value in $arr .所以我的方法是循环输入键中的每个字符,并在$arr中查找相应的值。 Each iteration will set found array element into variable $result .每次迭代都会将找到的数组元素设置为变量$result If the searched key does not exist (ex: "021"), print error message.如果搜索到的键不存在(例如:“021”),则打印错误消息。

<?php

$arr = [
    0 => [
        0 => "1-1",
        1 => "1-2",
        2 => "1-3",
        3 => [
            0 => "1-4-1",
            1 => "1-4-2",
            2 => "1-4-3"
        ]
    ],
    1 => [
        0 => "2-1",
        1 => "2-2",
        2 => "2-3"
    ],
    2 => [
        0 => "3-1",
        1 => "3-2",
        2 => "3-3",
        3 => [
            0 => "3-4-1",
            1 => "3-4-2"
        ]
    ],
];

$input = "230";

$result = $arr;

for ($i = 0; $i < strlen($input); $i++) {
    if (is_array($result)) {
        $result = $result[$input[$i]];
    } else {
        $result = 'Can not traverse path';
        break;
    }
}

echo $result;

After splitting the keys拆分密钥后

for($i=0;$i<strlen($keys);$i++){
$arr = $arr[$keys[$i]];
}
if(is_array($arr)){
echo json_encode($arr);
}else{
echo $arr;
}

You need a loop, which will go through the keys one by one and assigning into the array.您需要一个循环,它将 go 通过键一一分配到数组中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM