I am working with a one dimensional array in PHP. I would like to detect the presence of duplicate values, then count the number of duplicate values and out put the results. For example, given the following array: I would like to print: Any advice on how to approach this problem? Thanks. Mike Answer You can use array_count_values function will
Tag: arrays
PHP recursive search and replace array elements
I want to recursively search and replace elements in an array. The array is tree based so looks like etc. I want to be able to replace certain items with other items, so for example, I want to replace all entries in the array (at whatever depth level) of type A with an array of type B. But here’s the
Split text using multiple delimiters into an array of trimmed values
I’ve got a group of strings which I need to chunk into an array. The string needs to be split on either /, ,, with, or &. Unfortunately it is possible for a string to contain two of the strings which needs to be split on, so I can’t use split() or explode(). For example, a string could say first
How to bind mysqli parameters using loop and store results in array?
The code above gets the value from genreID when dbGenre is equal to $genre. And then store the results in an array. But it’s not working because $genre is an array, so I need to loop through it to get a different value from genreID each time. The ‘genres’ table contains two columns: genreID (INT) and dbGenre (VARCHAR) I just
How do you reindex an array in PHP but with indexes starting from 1?
I have the following array, which I would like to reindex so the keys are reversed (ideally starting at 1): Current array (edit: the array actually looks like this): How it should be: Answer If you want to re-index starting to zero, simply do the following: If you need it to start at one, then use the following: Here are
Checking to see if one array’s elements are in another array in PHP
I have two arrays in PHP as follows: People: Array ( [0] => 3 [1] => 20 ) Wanted Criminals: Array ( [0] => 2 [1] => 4 [2] => 8 [3] => 11 [4] => 12 …
Sorting an array of arrays by the child array’s length?
What’s the best way in PHP to sort an array of arrays based on array length? e.g. Answer This will work: Note that this function will preserve the numerical keys. If you want to reset the keys, wrap it in a call to array_values().
Convert a String to Variable
I’ve got a multidimensional associative array which includes an elements like I’ve got a strings like: How can I convert the strings into a variable to access the proper array element? This method will need to work across any array at any of the dimensions. Answer Quick and dirty: Of course the input string would need to be be sanitized
How to remove duplicate values from an array in PHP
How can I remove duplicate values from an array in PHP?
Is there a way to find out how “deep” a PHP array is?
A PHP array can have arrays for its elements. And those arrays can have arrays and so on and so forth. Is there a way to find out the maximum nesting that exists in a PHP array? An example would be a function that returns 1 if the initial array does not have arrays as elements, 2 if at least