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
Tag: arrays
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
Create PHP array from MySQL column
mysql_fetch_array will give me an array of a fetched row. What’s the best way generate an array from the values of all rows in one column?
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
PHP: Built-in function to check whether two Array values are equal ( Ignoring the order)
Is there a built-in function for PHP for me to check whether two arrays contain the same values ( order not important?). For example, I want a function that returns me true for the following two inputs: Edit: I could have sorted the two arrays and compare them, but as I am such a lazy guy, I would still prefer
What is the best way to split a string into an array of Unicode characters in PHP?
In PHP, what is the best way to split a string into an array of Unicode characters? If the input is not necessarily UTF-8? I want to know whether the set of Unicode characters in an input string is a subset of another set of Unicode characters. Why not run straight for the mb_ family of functions, as the first
How to search Array for multiple values in PHP?
I need to get the keys from values that are duplicates. I tried to use array_search and that worked fine, BUT I only got the first value as a hit. I need to get both keys from the duplicate values, in this case 0 and 2. The search result output as an array would be good. Is there a PHP