This is 'redo' of the code as the previous answer didn't do what anyone wanted.
Requirement:
Given:
- 1) a
root such as $section[0]
- 2) an array of data to add (
$newData)
- 3) The key of an 'entry', with an empty array, to add the data to.
Result:
- Replace the current empty array with the
$newData.
As this is a tree, I use the term $node to refer to entries in it.
How I choose to approach it.
1) Imagine you have a class (TreeWalk) that can 'visit' every node in the tree.
2) It can run any callable (visitor) on the node.
3) The node can be modified by the visitor
So, given that the TreeWalk class exists then the means to do what is required becomes:
- Write a
visitor to identify the required node and update it.
That is what is provided here.
I will provide the 'TreeWalk' class but that is a 'general treewalk' class that is used to run the 'nodeProcessor' that will update the correct node.
Sources and Demonstration:
_Update: Added code to do what the OP asked for...
The Code (nodeProcessor):
1) The data it uses:
// new data to add to the node
$updateData = array( 'key' => 16, // This must be the correct type i.e string '16' will not match!
'data' => array('name666' => 'Kilroy_666',
'contents666' =>
'Contents_666',
'id666' => array())
);
2) The code to be run at every node in the tree
/*
* It can be any `callable`... I use a closure but it could be a function or a method
*
* It will use:
* o $tree : instance passed as a parameter
*
* o $tree->currentKey : identify which node to update
* o $tree->currentNode : a reference to the node identified by the currentKey
*
* o $tree->processorData : This can be any data that you need for this routine
* to do what you want.
*
* we have: array('key' => 16,
'data' => array('hello', 'node 16'))
*
* it needs to identify the correct node and update it with the new data
*/
$updateNode = function (TreeWalk $tree) {
if ($tree->currentKey === $tree->processorData['key']) {
$path = $tree->showCurrentPath();
$tree->currentNode = $tree->processorData['data'];
$tree->endTreeWalk = true;
return $path;
}
};
Run the code:
$tree = new TreeWalk($parent, $updateNode, $updateData);
$tree->treeWalk();
TreeWalk Interface:
<?php
/*
* The class can be found here: http://pastebin.com/wKuKrPTv
*/
Interface ITreeWalk {
/**
* This class processes `paths` is a tree made from arrays
*
* You can provide a 'nodeProcessor' which is amy `callable` that will be
* run against every node.
*
* Also available is the `current path` through the arrays to get to the `currentNode`.
*
*/
/**
* A reference to the current node so it can be updated
*
* @property mixed $currentNode
*/
/**
* The key of the currentData - readonly
*
* @propert mixed $currentKey
*/
/**
* The 'current' stack of nodes in this path to reach this `currentNode`
*
* This is a 'stack'. The 'path' is all the entries combined.
*
* Each path entry is: array('key' => $currentKey, 'data' => $currentNode)
*
* @property array $currentPath
*/
/**
* Available data to the nodeProcesor
*
* @property mixed $processorData
*/
/**
* The Output
*
* @property array $processorResults
*/
/**
* Terminate the processing of any further nodes
*
* @property boolean $endTreeWalk
*/
/**
* Build the class but do not run it...
*
* Provides a default NodeProcessor if you don't provide one.
* o The default processor builds string paths that look like array paths
*
* @param array $tree
* @param callable $processNode - optional
* @param processorData - optional
*/
// public function __construct(array &$tree,
// /* callable */ $processNode = null,
// /* mixed */ $processorData = null);
/**
* This routine makes this class rather useful as you can use any callable.
*
* The nodeProcessor accepts one parameter:
* o the instance of the 'TreeWalk'
*
* It has some useful items of data available to it:
* 1) The `currentNode` - this is a reference so the node can be updated
* 2) The `currentKey` - this is the `array index' of the 'currentNode'
*
* 3) The `processorData` - This is anything that you decide you need when processing
* the `currentNode`.
*
* 4) The full path that leads to this node.
* This is useful as it can be checked as part of the search conditions
* if you wish.
*
* @param callable $nodeProcessor
*/
public function setNodeProcessor(/* callable */ $nodeProcessor);
/**
* This is the data used by the `nodeProcessor` it can be anything.
*
* Normally, an array?
*
* @param mixed $data
* @return void
*/
public function setProcessorData($data);
/**
* Any walue returned from the 'nodeProcessor' will be stored added to this array.
*
* Return a list of results were generated by the 'nodeProcessor'
* @return array
*/
public function results();
/**
* Process all the nodes.
*
* @param mixed optional - The data to be used by the `nodeProcessor`
*
* @return void
*/
public function treeWalk($processorData = null);
/**
* show the current path as text
*
* What if you don't use `foreach` - is it easier to understand? It is for me...
*
* Path stucture:
* 1) root node (required)
* 2) an iteration of intermediate nodes (optional) - makes sense
* 3) end data node (optional) !
* - what? yes - what if path is just the root ;-/
*
* @return string
*/
public function showCurrentPath();
/**
* If you don't provide a callable to generate paths then this will be used.
*
* It generates a list string paths to all the leaf nodes.
*
* @return string
*/
// public function defaultNodeProcessor();
}
end()moves the pointer.. the next step is to get the key viakey()you may consider first walking/traversing the array structure and returning a pointer/reference.. then work with the reference.id1is an array. Its index should be mentioned right before callingid2Like$section[0]['id1'][0]['id2'][0]['id3']. I want like this, but for an example I have mentioned 0 as an index. In that place of 0 I want last element index of that arrays