Skip to content
Advertisement

Tag: recursion

Recursive function and add to an array

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

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 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

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:

Advertisement