John Davidson

php - Multidimensional Array Search by Multiple Keys given as Variable?

0 comments
Message:


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


My Answer:
$value

Here is a sample code to achieve this:

php

// Sample multidimensional array
$array = [
1 => [
2 => [
3 => 'value'
]
]
];

// User input
$input = 230;

// Convert input to array of keys
$keys = str_split($input);

// Function to search multidimensional array by keys
function searchArrayByKeys($array, $keys) {
$result = $array;

foreach ($keys as $key) {
if (is_array($result) && array_key_exists($key, $result)) {
$result = $result[$key];
} else {
return null;
}
}

return $result;
}

// Search array by keys
$value = searchArrayByKeys($array, $keys);

// Output the result
echo $value;

?>


In this code, the function `searchArrayByKeys` takes the multidimensional array and an array of keys as input. It iterates through the keys and checks if the key exists in the current level of the array. If it exists, it moves to the next level of the array. If the result is a value (not an array), it returns the value. Finally, the code outputs the value found by searching the array with the given keys.

Rate this post

3 of 5 based on 9051 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us