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

Object Children Object type A Object type B Object Children Object type A Object


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 catch: The newly replaced objects may also have children of type A that need to be replaced.

So far I've got

foreach($nodes as &$node) { // Replace node? if($node['type'] == 'RefObject') { $n = $this->site->get_node_where('id', $node['node_ref']); // Replace node $node = $this->site->get_node_where('object_id', $n['object_id']); // Get children $node['children'] = $this->site->get_descendants($node['lft'], $node['rgt']); } } return $nodes;

Which will replace the first level of RefObjects, but will not search the subsequently added children.

I've been bashing my head against a wall with this one for hours. Please help!

Cheers, Gaz.


Put your code into a function and call it again. Pseudocode:

function checkArray($array) { ... if (is_array($node)) { // or whatever other criterium checkArray($node); // same function } }

The basics of recursion are to call the same code again...


you need to add this code into a function and call the function on the child nodes.

something like this (note the parseNodes function is called again inside the function):

function parseNodes($node) { foreach($nodes as &$node) { // Replace node? if($node['type'] == 'RefObject') { $n = $this->site->get_node_where('id', $node['node_ref']); // Replace node $node = $this->site->get_node_where('object_id', $n['object_id']); // Get children $node['children'] = parseNodes($this->site->get_descendants($node['lft'], $node['rgt'])); } } return $nodes; }



PHP 5.3 gets an <strong>array_replace_recursive</strong> method.

I just hope you'll be able to use it ;)



here's a recursive solution

function makeObject($array){ $data = false; foreach($array as $key=>$value){ if(is_array($value)){ $value = makeObject($value); } $data -> {$key} = $value; } return $data; }

thanks for getting me there!


  • jsTree : Append child node dynamically
  • PHP Handling Namespace with SimpleXML
  • Dealing with XElement null value
  • Angular2 emit event up to the DOM tree
  • Firebase, only get new children
  • Create an Event Handler whenever a new Node is added in treeview
  • Binary Tree Traversal Sum Of Each Depth
  • Neo4j: Legacy Indexes and auto index vs new label bases schema indexes
  • How to add learning rate to summaries?
  • d3.js selection conditional rendering
  • Extract All Possible Paths from Expression-Tree and evaluate them to hold TRUE
  • XSLT foreach repeating nodes to flat
  • Simple linked list-C
  • During installation of Django, why do I keep getting ImportError: No module named django?
  • pillow imaging ImportError
  • SAXReader not re-ecape characters
  • How to disable all widgets inside Panel or inside Composite?
  • List images(01.png) and descriptions(01.txt) from directory
  • CakePHP ACL tutorial initDB function warnings
  • Date Conversion from yyyy-mm-dd to dd-mm-yyyy
  • Debug.DrawLine not showing in the GameView
  • D3 nodes and links from JSON with nested arrays of children
  • Checking free space on FTP server
  • Avoid links criss cross / overlap in d3.js using force layout
  • All Classes Conforming to Protocol Inherit Default Implementation
  • CSS Linear-gradient formatting issue accross different browsers
  • How to make a tree having multiple type of nodes and each node can have multiple child nodes in java
  • Websockets service method fails during R startup
  • Rearranging Cells in UITableView Bug & Saving Changes
  • R: gsub and capture
  • AT Commands to Send SMS not working in Windows 8.1
  • Circular dependency while pushing http interceptor
  • Linker errors when using intrinsic function via function pointer
  • FormattedException instead of throw new Exception(string.Format(…)) in .NET
  • Observable and ngFor in Angular 2
  • How can i traverse a binary tree from right to left in java?
  • How to Embed XSL into XML
  • UserPrincipal.Current returns apppool on IIS
  • Conditional In-Line CSS for IE and Others?
  • Python/Django TangoWithDjango Models and Databases