1

we have an array which looks something like this

<?php
    $list = array(
        'liquid'=>array('water','soft drink'),
        'solid'=>array('car','computer','floor'),
        'gas'=>array('air','oxygen','carbon dioxide'),
    );
?>

now this is just an example list, what we are trying to achieve is

a user passes a value in a function like this

<?php
    function return_state($matter_value){
        return array_search($matter_value, $list);
    }
?>
  • user passes water the result should be liquid
  • user passes floor the result should be solid

in short whatever user is passing it will return the key associated with it

but when we are executing this function, it returns ''(empty value).

What are we doing wrong ?

3
  • 1
    You should also get an error telling you that $list doesn't exist, because it is out of scope for your function, so you need to pass it to the function as an argument Commented May 16, 2016 at 7:19
  • Then use a foreach loop to iterate over each top-level array value, and do your array_search, returning only if a match is found Commented May 16, 2016 at 7:19
  • @runningmark, You need to check answers Frayne Konok Commented May 16, 2016 at 8:01

4 Answers 4

2

Just a foreach loop with in_array makes this easy.

$list = array(
        'liquid'=>array('water','soft drink'),
        'solid'=>array('car','computer','floor'),
        'gas'=>array('air','oxygen','carbon dioxide'),
    );
function return_state($matter_value, $list){    
    foreach($list as $key => $val){
        if(in_array($matter_value, $val))
            return $key;        
    }
    return 'Not found.';
}

echo return_state('floor', $list); //solid

Pass your array through the function call.

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

Comments

1

Use foreach loop and in_array function:

function return_state($matter_value = ""){
    $list = [
        'liquid' => ['water','soft drink'],
        'solid' => ['car','computer','floor'],
        'gas' => ['air','oxygen','carbon dioxide'],
    ];
    if (!empty($matter_value)) {   // avoids empty arguments
        foreach ($list as $key => $items) {
            if (in_array($matter_value, $items)) {
                return $key;
            }
        }
    }
    return false;
}

print_r(return_state("water"));   // "liquid"
print_r(return_state("floor"));   // "solid"

1 Comment

trim returns the trimmed string. You should assign it to the matter_value variable, otherwise the function call is useless. after assigning it you can use the empty function to make sure the string is not empty
0

array_search not able to find in inner arrays. It will look into main array i.e. liquid, solid, gas and its values (not an array). You need loop through keys of main array to search key.

<?php
    function return_state($matter_value,$list){
        foreach($list as $key=>$item){
            $test=array_search($matter_value, $item);
            if($test!==FALSE){
                return $key;
            }
        }
    }
?>

Comments

0

Here's another solution to the problem. It makes the matter_value an array and intersects it with every element of the $list array. If nothing matched, the result will be null.

function return_state($matter_value, $list) {
    foreach ($list as $list_item_key => $list_item) {
        if ( ! empty(array_intersect([$matter_value], array_values($list_item)))) {
            return $list_item_key;
        }
    }
}

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.