0

I have an array of objects. I need to loop over these objects (preferably without foreach(), and if a certain key in the objects is not empty, then return true, otherwise return false.

For example,

$items = array(
    '0' => stdClass {
        name => Ryan
        suppliers => array()
    }
    '1' => stdClass {
        name => Dave
        suppliers => array(
            '0' => stdClass {}
        )
    }
)

Essentially, I need to go through the array and check the "supplier" key of the object, and if any of them are not empty, return true for the entire thing, otherwise return false.

5
  • 4
    What's wrong with foreach()? Commented May 4, 2017 at 15:08
  • I need to go through the array and check the "supplier" key of the object, and if any of them are not empty, return true for the entire thing, otherwise return false. so what's the question? Commented May 4, 2017 at 15:09
  • You might want to tell us why you don't want to use foreach and show us what you've tried so far. Commented May 4, 2017 at 15:10
  • 1
    and foreach is the best solution because you can return false immediately after you find empty suppliers Commented May 4, 2017 at 15:10
  • you may want to use array_walk_recursive() Commented May 4, 2017 at 15:17

3 Answers 3

3

What's wrong with foreach?

$check = function($arr) {
  foreach($arr as $o) {
     if (!empty($o->suppliers)) return true;
  }
  return false;
};

If you want to use it only in one place, use anonymous function

I don't understand why you don't want to use foreach because thing is - foreach is only right way because you leave loop as soon as you find value

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

5 Comments

I'm looking at just placing it in an if statement, rather than a function (its the only place its used on the site, and it will only ever be that place. I'd use array_walk but I'm not sure thats the best?
@RyanHipkiss use anonymous function then (see my post)
@MagnusEriksson it stores function in variable, you can using against array $ok = $check($my_array)
Depending on your PHP version, you can execute the function straight off: $check = (function ($arr) { //foreach...})($arr);
You started it!;-) But yes, since it's a one off, simply set $check as false and set it as true and break the loop if the if-statement validates as true. I'm not really getting what actual issue the OP has and why all the hate for foreach. @RyanHipkiss There are a lot of fancy array functions, but be warned, a lot of them is way more performance heavy than foreach.
0

One other option, reduce the array to a boolean.

array_reduce($items, function($hasSupplier, $item) {
    return !empty($item->suppliers) || $hasSupplier;
});

Still, I prefer the foreach solution since it won't continue to iterate unnecessarily.

Comments

0

You can filter and check for a result:

if(array_filter($items, function($v) { return !empty($v->suppliers); })) {
    //at least one not empty
} else {
    //all are empty
}

If you really want a boolean:

$result = (bool)array_filter($items, function($v) { return !empty($v->suppliers); })

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.