I have a multidimensional array $md_array and I want to add more elements to the sub-arrays recipe_type and cuisine coming from a loop that reads data from a table. In the loop, I create a new table $…
Tag: multidimensional-array
Collect all values in a multidimensional array with a specific key
I have an array like this: How can I get all [id] values from this array and store them in a flat array like this: Answer $a is your original array.
Count occurrences of a specific value in multidimensional array
Let’s say I have a multidimensional array like this: How would I be able to count how many times the value “Thing1” occurs in the multidimensional array? Answer Try this : Output : It gives the occurrence of each value. ie : Thing1 occurs 2 times. EDIT : As per OP’s comment : “Which array do you mean resulting array?”
usort descending
When i try to apply the below code from here it gives me results in ascending order. Output: On swapping $a and $b it gives the results in descending order except one value Output: i want to have the results in the below order: How do i achieve the same.? Answer My first guess is that usort expects an integer
replace array keys with given respective keys
I have an array like below I have another array having keys to replace with key information. I need to replace all keys of array $old with respective values in array $keyReplaceInfo. Output should be like this I had to do it manually as below. I am expecting better option. can anyone suggest better way to accomplish this? I know
php multidimensional array get values
This is my array in php $hotels Array ( [0] => Array ( [hotel_name] => Name [info] => info [rooms] => Array ( [0] => …
PHP recursive array copy
I have a multidimensional array of arrays (source below), where my keys and values are arbitrary strings. I want to create a new array (desired below), where the hierarchy stays the same, but every …
Merge two indexed arrays of indexed arrays based on first column value
I have two arrays like this: How can I merge these two arrays without looping? Is there any php functionality available for this? I need this kind of output: Answer You can use the PHP function array_merge_recursive. See the example:
array_uintersect() gives unexpected results when callback only returns 0 or 1
I have a custom callback in my array_uintersect() call because I need to case-sensitively compare strings in two elements while comparing rows between two multi-dimensional arrays. OUTPUT I can’t understand why this code doesn’t produce the correct output which should be an array with “B”, “C” and “D” elements, because only “A” element is different from $vieja to $nueva… if
Inserting a multi-dimensional php array into a mysql database
I have an array from a csv with a similar structure to this: I would like to insert it into a MySQL table where the items of the first array (name, age, gender) are the column titles and each subsequent array is a row in the table. Could anyone advise as to the best way to do this? Answer The