0

I have this simple array in PHP that I need to filter based on an array of tags matching those in the array.

Array
(
    [0] => stdClass Object
        (
            [name] => Introduction
            [id] => 798162f0-d779-46b6-96cb-ede246bf4f3f
            [tags] => Array
                (
                    [0] => client corp
                    [1] => version 2
                )
        )

    [1] => stdClass Object
        (
            [name] => Chapter one
            [id] => 761e1909-34b3-4733-aab6-ebef26d3fcb9
            [tags] => Array
                (
                    [0] => pro feature
                )
        )
)

When supplied with 'client corp', the output should be the above array with only the first item.

So far I have this:

$selectedTree = array_filter($tree,"checkForTags");

function checkForTags($var){

   $arr = $var->tags;
   $test = in_array("client corp", $arr, true);

   return ($test);
}

However, the result is that it's not filtering. When I echo $test, I get 1 all the time. What am I doing wrong?

3
  • Try using the array_map() function instead of the array_filter() function: php.net/array_map Commented Nov 20, 2014 at 15:52
  • Are you sure $arr does not always contains "client corp"? Commented Nov 20, 2014 at 15:54
  • to me your code works codepad.org/AvpbTE9W Commented Nov 20, 2014 at 16:25

2 Answers 2

1

Something like this should do the trick:

$selectedTree = array_filter(array_map("checkForTags", $tree ,array_fill(0, count($tree), 'client corp')));

function checkForTags($var, $exclude){
   $arr = $var->tags;
   $test = in_array($exclude, $arr, true);
   return ($test ? $var : false);
}

array_map() makes sure you can pass arguments to the array. It returns each value altered. So in the returning array, some values are present, others are set to false. array_filter() with no callback filters all falsey values from that array and you are left with the desired result

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

6 Comments

Sorry... small typo there... Updated the answer
why is there both an array_filter and an array_map?
@greener array_map() makes sure you can pass arguments to the array. It returns each value altered. So in the returning array, some values are present, others are set to false. array_filter() with no callback filters all falsey values from that array and you are left with the desired result
Thanks for the explanation. When I replace the array values with the json payload array(Input::json("selectedTags")) it filters out everything again. Something small is missing but your answer is correct.
Upon further testing, it looks like there are issues with this approach. If I pass pro feature as the argument, the returned array is empty when it should instead show the second item of the initial array.
|
0

The in_array() function returns TRUE if needle is found in the array and FALSE otherwise. So by getting 1 as a result that means that "client corp" is found.

Check PHP in_array() manual

You can user array_search() to return the array key instead of using in_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.