Introduced
Normally our foreach can print each key => value in a one-dimensional array sequentially, but if it is a multidimensional array, it needs to be cycled in a nested loop, or recursive implementation, but these methods are not flexible enough, because when the array is not determined to be a few dimensions, There is no never-ending loop of nesting, if recursive is a solution, but how do you do it if you just want to use a foreach loop?
Implementation Way One
using PHP itself with the iterator class Recursiveiteratoriterator
$test _arr = Array (1,2,3,array (4, ' AA ' =>5,6,array (7, ' BB ' =>8), 9,10), 11,12);
$arrayiter = new Recursivearrayiterator ($test _arr);
$iteriter = new Recursiveiteratoriterator ($arrayiter);
Direct printing can be printed in a horizontal order
. foreach ($iteriter as $key => $val) {
echo $key. ' => '. $val;
}
Results/
*
0=>1
1=>2
2=>3
0=>4
aa=>5
2=>6 Bb=>8
4=>9
5=>10
4=>11
5=>12
* *
Implementation Mode Two
Implement a recursiveiteratoriterator-like iterator class to realize multidimensional array horizontal printing function
Class Foreachprintfarr implements iterator {//current array scope private $_items;
Private $_old_items;
Save every execution array environment stack private $_stack = array ();
Public function __construct ($data =array ()) {$this->_items = $data;
Private Function _isset () {$val = current ($this->_items);
if (Empty ($this->_stack) &&! $val) {return false;
else {return true;
The public Function current () {$this->_old_items = null;
$val = current ($this->_items);
If it is an array, it saves the current execution environment and then switches to the new array execution Environment if (Is_array ($val)) {Array_push ($this->_stack, $this->_items);
$this->_items = $val;
return $this->current (); //Determine if the current execution needs to be cut back to the last execution Environment//(1) If there is a jump to continue execution/(2) if it does not exist and the environment stack is empty, the current execution to the last element//(3) If an element is not present in the current array environment
In, save the current execution array environment $this->_old_items = $this->_items; Then switch the last execution environment $this->_items = Array_pop ($this->_stack) continue to loop, straightThe next//element is not empty until the current array environment while (1) {if (Next ($this->_items)) {prev ($this->_items); Bre
Ak
} elseif (Empty ($this->_stack)) {End ($this->_items);
else {end ($this->_items);
if (! $this->_old_items) $this->_old_items = $this->_items;
$this->_items = Array_pop ($this->_stack);
} return $val;
Public function Next () {Next ($this->_items); The Public Function key () {//Because the key () function executes after the current () function//So switches the execution environment in the current () function, which results in the previous execution environment before switching the last K EY//becomes the key after switching, so $this->_old_items Save the execution environment before or after the switch to prevent key printing error return $this->_old_items?
Key ($this->_old_items): Key ($this->_items);
The Public Function rewind () {Reset ($this->_items);
The public function valid () {return $this->_isset ();
}
}
Internal execution mode
1, the Foreach Loop our custom Foreachprintfarr class, will automatically call the internal 5 methods valid (), rewind (), key (), next (), current () we only need to implement these several methods.
2, call Order:
1th time => Rewind-> valid-> current-> key
2nd time ~n => next-> valid-> current-> key
$test _arr = Array (1,2,3,array (4, ' AA ' =>5,6,array (7, ' BB ' =>8), 9,10), 11,12);
$iteriter = new Foreachprintfarr ($test _arr);
foreach ($iteriter as $key => $val) {
echo $key. ' => '. $val;
}
Results:/
*
0=>1
1=>2
2=>3
0=>4
aa=>5
2=>6 Bb=>8
4=>9
5=>10
4=>11
5=>12
* *
The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.