Home > Mobile >  Multidimensional Array Search by Multiple Keys given as Variable?
Multidimensional Array Search by Multiple Keys given as Variable?

Time:11-24

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

//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?

CodePudding user response:

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. Each iteration will set found array element into variable $result. If the searched key does not exist (ex: "021"), print error message.

<?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;

CodePudding user response:

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.

  • Related