1

do you know how to test, whether an index, based on what we pass in a string, exists in an array?

Assume the following code:

<?php

$myArray = [
    "elemOne" => "valueElemOne",
    "elemTwo" => [
        "elemTwoOne" => "valueElemTwoOne",
        "elemTwoTwo" => [
            "elemThreeOne" => "valueElemThreeOne",
            "elemThreeTwo" => "valueElemThreeTwo",
        ],
    ],
];

Now, I have a string $myString = "elemTwo/elemTwoTwo/elemThreeThree". What I want to do with $myString is somewhat format it in a way, so I could check

<?php

if(isset($myArray['elemTwo']['elemTwoTwo']['elemThreeThree'])) {
    // maybe do something
    return true;
} else {
    return false;
}

Naturally in my case this would return false, as the index "elemThreeThree" does not exist within my array. I tried splitting the string, tried to format as [elemTwo][elemTwoTwo][elemThreeThree] and then evaluating it, but nothing really worked.

Do you think of a possible approach that could help me?

2 Answers 2

1

simple solution could be

  <?php
     $arr = array('hello'=>array('world'=>array('in'=>'jo')));
     $myString = "hello/world/inf";

     function checkString($string, array $search_array) {
        $test = explode('/',$string);
        foreach($test as $key) {
           if(isset($search_array[$key])) $search_array = $search_array[$key];
           else return false;
        }
        return true;
     }
     var_dump(checkString($myString,$arr));
     die;

The Output of the function will true or false

Sign up to request clarification or add additional context in comments.

Comments

0

Why did splitting it not work? Is there something wrong with this?

<?php
$arrayTest = array(
    "arrayOne" => "string", 
    "arrayTwo" => array(
        "arrayThree"    => "string",
        "arrayFour"     => array("winner" => "me")
    )
);

var_dump(expanded_key_exists($arrayTest, "arrayOne"));
var_dump(expanded_key_exists($arrayTest, "arrayTwo"));
var_dump(expanded_key_exists($arrayTest, "arrayTwo/arrayThree"));
var_dump(expanded_key_exists($arrayTest, "arrayTwo/arrayThree/arrayTwenty"));
var_dump(expanded_key_exists($arrayTest, "arrayTwo/arrayFour"));
var_dump(expanded_key_exists($arrayTest, "arrayTwo/arrayFour/winner"));

if(expanded_key_exists($arrayTest, "arrayTwo/arrayFour/winner")){
    echo "The winner is: " . expanded_array_key($arrayTest, "arrayTwo/arrayFour/winner");
}

function expanded_key_exists($array, $key){
    $arrayKeys = explode("/", $key);
    foreach($arrayKeys as $someKey){
        if(is_array($array) && array_key_exists($someKey, $array)){
            $array = $array[$someKey];
        }else{
            return false;
        }
    }
    return true;
}

function expanded_array_key($array, $key){
    $arrayKeys = explode("/", $key);
    foreach($arrayKeys as $someKey){
        if(is_array($array) && array_key_exists($someKey, $array)){
            $array = $array[$someKey];
        }else{
            return null;
        }
    }
    return $array;
}
?>

2 Comments

The output btw: bool(true) bool(true) bool(true) bool(false) bool(true) bool(true) The winner is: me
Thanks a lot Christian I could not wrap my head around the problem.

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.