0

I'm using array_filter with multiple parameters but it's not doing the filter correctly. Here where it's supposed to return an array with only "arts,crafts,designs" as an element, it's returning an empty array. The only $askedcat parameter it works for is "arts". I can't find out what the issue is.

I've tried not using an array_filter and instead just looping over the array, and I get the same problem.

class CategoryFilter {
    public $categoryAskedFor;

    function __construct($askedCat) {
            $this->categoryAskedFor = $askedCat;
    }

    function categoryCallback($projectCategoryString) {
        $project_category_array = explode(",", $projectCategoryString);
        if(in_array($this->categoryAskedFor, $project_category_array)) return true;
        return false;
    }
}

$verifiedProjects = ["arts", "arts,crafts,designs", "film", "film,theater"];

$askedCat = "crafts";

$newArr = array_filter($verifiedProjects, array(new CategoryFilter($askedCat), "categoryCallback"));

for ($i = 0; $i < count($newArr); $i++) {
    echo $newArr[$i] . "<br>";
}

I expect the output here to be arts,crafts,design<br> but it's only <br> meaning the array is empty.

6
  • 2
    $this->categoryAskedFor instead of $this->$categoryAskedFor in __construct…!? Commented Oct 28, 2019 at 15:21
  • 1
    What @deceze said plus the fact that you're looping over your array wrong. Use foreach (array_filter will preserve keys so the 0 index is not necessarily there after filtering). Commented Oct 28, 2019 at 15:22
  • 2
    Please go enable proper PHP error reporting. (Go read up on it, if you don’t know what that means.) PHP could alert you to a lot of these issues on its own already, before you need to go bother someone else with them. Commented Oct 28, 2019 at 15:23
  • Edited above @deceze wasn't my issue, just happened when I copied the code over here Commented Oct 28, 2019 at 15:23
  • @Jeto are you referencing the loop that prints the values? Commented Oct 28, 2019 at 15:25

4 Answers 4

1

There are many way to achieve this but let me show you two way here

WAY #1

If you using the for loop to retrieve the array value then need to have numeric key and as per your code you need array_values function to manage that

<?php

    class CategoryFilter {
        public $categoryAskedFor;

        function __construct($askedCat) {
                $this->categoryAskedFor = $askedCat;
        }

        function categoryCallback($projectCategoryString) {
            $project_category_array = explode(",", $projectCategoryString);
            if(in_array($this->categoryAskedFor, $project_category_array)) return true;
            return false;
        }
    }

    $verifiedProjects = ["arts", "arts,crafts,designs", "film", "film,theater"];

    $askedCat = "crafts";

    $newArr = array_filter($verifiedProjects, array(new CategoryFilter($askedCat), "categoryCallback"));

    $newArr = array_values($newArr);
    for ($i = 0; $i < count($newArr); $i++) {
        echo $newArr[$i] . "<br>";
    }

WAY # 2

If you don't want to use the array_values here then you need to manage the foreach loop instead of for loop

<?php

    class CategoryFilter {
        public $categoryAskedFor;

        function __construct($askedCat) {
                $this->categoryAskedFor = $askedCat;
        }

        function categoryCallback($projectCategoryString) {
            $project_category_array = explode(",", $projectCategoryString);
            if(in_array($this->categoryAskedFor, $project_category_array)) return true;
            return false;
        }
    }

    $verifiedProjects = ["arts", "arts,crafts,designs", "film", "film,theater"];

    $askedCat = "crafts";

    $newArr = array_filter($verifiedProjects, array(new CategoryFilter($askedCat), "categoryCallback"));

    foreach ($newArr as $value) {
        echo $value . "<br>";
    }
Sign up to request clarification or add additional context in comments.

Comments

0

The way you're looping over your resulting array is wrong, because array_filter will preserve the array keys. The 0 index may not be there in the filtered version (and in your case, it actually isn't).

Use foreach instead:

foreach ($newArr as $value) {
  echo $value, '<br>';
}

3 Comments

I think you meant echo $value . '<br>';
@Claudio Nope, echo takes any number of parameters, this way you don't have to build/buffer a string before outputting it.
Great, didn't knew that
0

array_filter will remove elements but will not reset the keys. use array_values to reset the keys first.

$newArr = array_values($newArr);
for ($i = 0; $i < count($newArr); $i++) {
    echo $newArr[$i] . "<br>";
}

1 Comment

Thank you, while foreach solved the issue, this is very helpful!
0

You will get as per your output

$askedCat = 'crafts';
$verifiedProjects = ["arts", "arts,crafts,designs", "film", "film,theater"];
$newArr = array_filter($verifiedProjects, function ($item) use ($askedCat) {
    if (stripos($item, $askedCat) !== false) {
        return true;
    }
    return false;
});

foreach ($newArr as $value) {
        echo $value . "<br>";
    }

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.