I need recursive function to add element into an array. This is my code: Basically, if I echo $new_x and $new_y, I get what I’m expecting. Problem is my $result array, it gives me only first item (2 -2) but second is missing (1 -1). I guess problem is with adding item into array but cannot find the problem. Answer
Tag: recursion
Filter array with array_walk_recursive but deny specific values
I am trying to filter an array This returns me an array of all array-fields having id as key. But there is a small problem, some of the array have an key called attribute containing an idkey field that I dont want to have. Is there a way to say “don’t take the id inside attribute” ? My current solution,
How to create nested array ancestors recursively in PHP
I’m using PHP 7.3. I want to create a nested array that each item must contain its own ancestors. Original Array: Required Array: I tried to use this solution, but I think this problem is different. Any help / guidance is greatly appreciated! Thanks. Answer You could first make use of array_column to group your entries by ID, then array_reduce
How to get proper debug context from production PHP code? print_r vs var_export vs var_dump corner cases
Requirements We have production PHP code running on a webserver. In some sitations we want to enrich debug output (going e.g. to error.log) with context information. The context can have arbitrary …
Determine if specific value exists in any level of a multidimensional array
For this nested array: I want to know if a certain value “ERROR” exists at least once in the array. I don’t care what key it’s associated with, I just want to know if the $status array contains an “ERROR” status anywhere in it. Is there a cleaner way to do this than just iterating over the elements with nested
String to multidimensional/recursive array in PHP
I have a problem with creating a recursive array with PHP. I need to format this string to a multidimensional array with the dot-separated elements indicating multiple levels of array keys. $str = “…
How can I recursively search for and replace values inside of an unknown-depth multidimensional PHP array?
I’m working with a JSON string. I’m converting it to an associative array to find specific values and change those values when a certain key is found ([‘content’]). The depth of the array is always unknown and will always vary. Here is the function I wrote. It takes an array as an argument and passes it by reference so that
Insert spaces into a string at each change of character
I’m trying to create a string with a blank space between all “different characters, for example: I tried the following recursive function, not knowing if this is the best way because I couldn’t find any build-in function in PHP for this. For some reason, this function seems to iterate infinitely, and I can’t figure out why. Where do I go
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.
Get category tree recursively (Yii)
I have a table with following structure: categories id name parent_id I want to get the tree of categories with single function. I’ve written something like this in my model, but it doesn’t work. Can someone help me with this? Thanks. Answer I’ve finally solved the issue. If someone interested, here’s the code: