note 120131 added to class.recursiveiteratoriterator

From: Date: Tue, 08 Nov 2016 10:15:31 +0000
Subject: note 120131 added to class.recursiveiteratoriterator
Groups: php.notes 
Request: Send a blank email to php-notes+get-207047@lists.php.net to get a copy of this message
A very useful use case for RecusiveIteratorIterator in combination with RecursiveArrayIterator is to replace array values on a multidimensional array at any level deep. Usually, array_walk_recursive would be used to replace values deep within arrays, but unfortunately this only works when there is a standard key value pair - in other words, array_walk_recursive ONLY VISITS LEAF NODES, NOT arrays. So to get around this, the iterators can be used in this way: <?php $array = [ 'test' => 'value', 'level_one' => [ 'level_two' => [ 'level_three' => [ 'replace_this_array' => [ 'special_key' => 'replacement_value', 'key_one' => 'testing', 'key_two' => 'value', 'four' => 'another value' ] ], 'ordinary_key' => 'value' ] ] ]; $arrayIterator = new \RecursiveArrayIterator($array); $recursiveIterator = new \RecursiveIteratorIterator($arrayIterator, \RecursiveIteratorIterator::SELF_FIRST); foreach ($recursiveIterator as $key => $value) { if (is_array($value) && array_key_exists('special_key', $value)) { // Here we replace ALL keys with the same value from 'special_key' $replaced = array_fill(0, count($value), $value['special_key']); $value = array_combine(array_keys($value), $replaced); // set a new key $value['new_key'] = 'new value'; // Get the current depth and traverse back up the tree, saving the modifications $currentDepth = $recursiveIterator->getDepth(); for ($subDepth = $currentDepth; $subDepth >= 0; $subDepth--) { // Get the current level iterator $subIterator = $recursiveIterator->getSubIterator($subDepth); // If we are on the level we want to change, use the replacements ($value) other wise set the key to the parent iterators value $subIterator->offsetSet($subIterator->key(), ($subDepth === $currentDepth ? $value : $recursiveIterator->getSubIterator(($subDepth+1))->getArrayCopy())); } } } return $recursiveIterator->getArrayCopy(); // return: $array = [ 'test' => 'value', 'level_one' => [ 'level_two' => [ 'level_three' => [ 'replace_this_array' => [ 'special_key' => 'replacement_value', 'key_one' => 'replacement_value', 'key_two' => 'replacement_value', 'four' => 'replacement_value', 'new_key' => 'new value' ] ], 'ordinary_key' => 'value' ] ] ]; ?> The key is in traversing back up the tree to save the changes at that level - simply calling $recursiveIterator->offsetSet(); will only set a key on the root array. ---- Server IP: 72.52.91.14 Probable Submitter: 81.27.92.130 ---- Manual Page -- http://php.net/manual/en/class.recursiveiteratoriterator.php Edit -- https://master.php.net/note/edit/120131 Del: integrated -- https://master.php.net/note/delete/120131/integrated Del: useless -- https://master.php.net/note/delete/120131/useless Del: bad code -- https://master.php.net/note/delete/120131/bad+code Del: spam -- https://master.php.net/note/delete/120131/spam Del: non-english -- https://master.php.net/note/delete/120131/non-english Del: in docs -- https://master.php.net/note/delete/120131/in+docs Del: other reasons-- https://master.php.net/note/delete/120131 Reject -- https://master.php.net/note/reject/120131 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#207047) next »