aurawindsurfing's avatar

Why there is no in_array for helpers?

Hi,

There is ->contains() for collections There is ->contains() for strings

Yet there is no way to check if array contains/has value like Arr::contains()

What am I missing?

0 likes
17 replies
Snapey's avatar

because you can use the native in_array() method or array_key_exists() depending on what you want?

jlrdw's avatar

@aurawindsurfing the actual method is:

    public function contains($key, $operator = null, $value = null)
    {
        if (func_num_args() === 1) {
            if ($this->useAsCallable($key)) {
                $placeholder = new stdClass;

                return $this->first($key, $placeholder) !== $placeholder;
            }

            return in_array($key, $this->items);
        }

        return $this->contains($this->operatorForWhere(...func_get_args()));
    }

https://github.com/laravel/framework/blob/9.x/src/Illuminate/Collections/Collection.php#L175

Trace down the other methods involved. Let the API be your friend.

tykus's avatar

@jlrdw that is a Collection method, not an Arr method 🤦‍♂️

tykus's avatar

@jlrdw

collections are array's

Nope! They are_a fluent, convenient wrapper for working with arrays_ - i.e. an Object.

tykus's avatar

@jlrdw Collections are not arrays. The Collection class has an $items property which is an array - don't confused the two.

Anyway, the question was answered by @snapey at the outset. If the OP wants a contains method on the Arr helper class; he can macro it.

Emokores's avatar

Sometimes, we treat Laravel like it's a language. Its still PHP! Whatever prebuilt methods are not in Laravel, means they are in PHP, so you can still use them as PHP methods

jlrdw's avatar

@Emokores I agree, laravel has shortcuts to php methods. Like the store method for images. Symfony in turn just uses php move in the background. (Just example).

Edit:

I deleted answers, But I was only agreeing, but was just trying to point out to look some things up in the API. Laravel in this case as snapey said doesn't include it because it's already a php method. Whereas a collection has a contains added.

jjudge's avatar

@Emokores Being able to use helpers that work equally on arrays and collections, adds a lot of flexibility when code is being upgraded or maintained. in_array() will only take an array. A helper function could take an array, a collection, or an Arrayable object.

aurawindsurfing's avatar

I agree about PHP methods but still it feels to me like a missing one from the Array helpers.

martinbean's avatar

@aurawindsurfing Why do you need a helper? It literally exists in PHP. You don’t need the framework to proxy an existing method just to be able to use it.

Sinnbeck's avatar

@aurawindsurfing You could add a macro to add it yourself. Put this in the boot method of a service provider. Code borrowed from the PR!

Arr::macro('contains', function($array, $needle) {
        return in_array($needle, $array instanceof Collection ? $array->flatten()->toArray() : static::flatten($array));
    });
aurawindsurfing's avatar

@martinbean I know it exists its just that Laravel spoiled me so much that this one feels odd to me that it is missing. You could say the sake thing for many other PHP functions and yet they exists as a helper. Its not a deal breaker just awkward it is not there.

Please or to participate in this conversation.