note 101145 modified in function.array-replace by danbrown

From: Date: Mon, 29 Nov 2010 18:10:29 +0000
Subject: note 101145 modified in function.array-replace by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-174449@lists.php.net to get a copy of this message
I got hit with a noob mistake. :) When the function was called more than once, it threw a function redeclare error of course. The enviroment I was coding in never called it more than once but I caught it in testing and here is the fully working revision. A simple logical step was all that was needed. With PHP 5.3 still unstable for Debian Lenny at this time and not knowing if array_replace would work with multi-dimensional arrays, I wrote my own. Since this site has helped me so much, I felt the need to return the favor. :) <?php // Polecat's Multi-dimensional array_replace function // Will take all data in second array and apply to first array leaving any non-corresponding values untouched and intact function polecat_array_replace( array &$array1, array &$array2 ) { // This sub function is the iterator that will loop back on itself ad infinitum till it runs out of array dimensions if(!function_exists('tier_parse')){ function tier_parse(array &$t_array1, array&$t_array2) { foreach ($t_array2 as $k2 => $v2) { if (is_array($t_array2[$k2])) { tier_parse($t_array1[$k2], $t_array2[$k2]); } else { $t_array1[$k2] = $t_array2[$k2]; } } return $t_array1; } } foreach ($array2 as $key => $val) { if (is_array($array2[$key])) { tier_parse($array1[$key], $array2[$key]); } else { $array1[$key] = $array2[$key]; } } return $array1; } ?> [I would also like to note] that if you want to add a single dimensional array to a multi, all you must do is pass the matching internal array key from the multi as the initial argument as such: <?php $array1 = array( "berries" => array( "strawberry" => array( "color" => "red", "food" => "desserts"), "dewberry" = array( "color" => "dark violet", "food" => "pies"), ); $array2 = array( "food" => "wine"); $array1["berries"]["dewberry"] = polecat_array_replace($array1["berries"]["dewberry"], $array2); ?> This is will replace the value for "food" for "dewberry" with "wine". The function will also do the reverse and add a multi to a single dimensional array or even a 2 tier array to a 5 tier as long as the heirarchy tree is identical. I hope this helps atleast one person for all that I've gained from this site. --was-- I got hit with a noob mistake. :) When the function was called more than once, it threw a function redeclare error of course. The enviroment I was coding in never called it more than once but I caught it in testing and here is the fully working revision. A simple logical step was all that was needed. With PHP 5.3 still unstable for Debian Lenny at this time and not knowing if array_replace would work with multi-dimensional arrays, I wrote my own. Since this site has helped me so much, I felt the need to return the favor. :) <?php // Polecat's Multi-dimensional array_replace function // Will take all data in second array and apply to first array leaving any non-corresponding values untouched and intact function polecat_array_replace( array &$array1, array &$array2 ) { // This sub function is the iterator that will loop back on itself ad infinitum till it runs out of array dimensions if(!function_exists('tier_parse')){ function tier_parse(array &$t_array1, array&$t_array2) { foreach ($t_array2 as $k2 => $v2) { if (is_array($t_array2[$k2])) { tier_parse($t_array1[$k2], $t_array2[$k2]); } else { $t_array1[$k2] = $t_array2[$k2]; } } return $t_array1; } } foreach ($array2 as $key => $val) { if (is_array($array2[$key])) { tier_parse($array1[$key], $array2[$key]); } else { $array1[$key] = $array2[$key]; } } return $array1; } ?> http://php.net/manual/en/function.array-replace.php

« previous php.notes (#174449) next »