0

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? Answer given by @Anggara I made it in to 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);
3
  • What does the variable '$value' has? Do you want , if key = 230 result will be 3-4-1? Commented Nov 24, 2021 at 13:32
  • 1
    I tried it by searching key and value just forget about the function might be to deep I have been trying things, and Yes if key = 230 result is 3-4-1 if exists. I also found the way to split the key I will edited the post @shirshak007 Commented Nov 24, 2021 at 13:36
  • I edited the Function yet to be successful now it prints all results :D Commented Nov 24, 2021 at 14:19

2 Answers 2

2

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;
Sign up to request clarification or add additional context in comments.

1 Comment

Thx , this is really helpful I will try to make it in to a function ^^
1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.