Skip to content
Advertisement

Tag: arrays

Check if an array contains a specific value?

I’d like to check if there is a value on an array like this: I’ve read the check_value_new method is a better way to work with arrays, but I’m not used to work with it, how I should fix it? Answer PHP offers a function called in_array that checks if a value exists in a given array. You can change

PHP case-insensitive in_array function

Is it possible to do case-insensitive comparison when using the in_array function? So with a source array like this: $a= array( ‘one’, ‘two’, ‘three’, ‘four’ ); The following lookups would all …

Passing arrays as url parameter

What is the best way that I can pass an array as a url parameter? I was thinking if this is possible: or how about this: Ive read examples, but I find it messy: Answer There is a very simple solution: http_build_query(). It takes your query parameters as an associative array: will return http_build_query() handles all the necessary escaping for

Explode string only once on first occurring substring

preg_match() gives one match. preg_match_all() returns all matches. preg_split() returns all splits. How can I split only on the first match? Example: This is what I want: Answer Simply set $limit to 2 for 2 parts of the array. Thanks to @BenJames for mentioning: I tested and it works fine. The limit argument: If specified, then only substrings up to

Pattern Match on a Array Key

I need to get the stock values out of this array: I need to pattern match on this array, where the array key = “stock” + 1 wildcard character. I have tried using the array filter function to get every other value on the PHP manual but the empty values seem to throw it out. I tried alot of different

How to filter an array by a condition

I have an array like this: Now I want to filter that array by some condition and only keep the elements where the value is equal to 2 and delete all elements where the value is NOT 2. So my expected result array would be: Note: I want to keep the keys from the original array. How can I do

Advertisement