3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?php class RecursionImplementation { public static function recursionInitiator($array, $keylist) { self::$keys = explode(",", $keylist); if(is_null(self::keys) || count(self::$keys) == 0) return $array; $array = self::recursion($array); return $array; } private static function recursion($array) { $all_keys = array_keys($array); $intersection = array_intersect($all_keys, self::$keys); $difference = array_diff($all_keys, self::$keys); $number_of_intersecting_keys = count($intersection); $number_of_non_intersecting_keys = count($array) - $number_of_intersecting_keys; if($number_of_intersecting_keys + $number_of_non_intersecting_keys == 0) return; if($number_of_intersecting_keys >= 1) { foreach($intersection as $key) { unset($array[$key]); $array[$key] = self::$asterisk; } } if($number_of_non_intersecting_keys >= 1) { foreach($difference as $key) { if(is_array($array[$key])) { $value = self::recursion($array[$key]); unset($array[$key]); $array[$key] = $value; } } } return $array; } private static $keys; private static $asterisk = "**"; } $information = array("login" => "someUsername", "password" => "somePassword", "card_pin"=>"card_1234", "pin"=>"pin_356", "iin" => "iin_123", "shipping" => array("value" => array("telephone" => array("inner_value" => "telephone_998"))), "cardnumber" => "card_number_876", "customer_id" => "customer_654", "telephone" => "someOtherTelephoneNumber555"); $list = "telephone,login"; print_r($information); echo "\n\n"; $final_array = RecursionImplementation::recursionInitiator($information, $list); print_r($final_array);
based on DpNdj
Output for 7.0.0 - 7.2.0
Array ( [login] => someUsername [password] => somePassword [card_pin] => card_1234 [pin] => pin_356 [iin] => iin_123 [shipping] => Array ( [value] => Array ( [telephone] => Array ( [inner_value] => telephone_998 ) ) ) [cardnumber] => card_number_876 [customer_id] => customer_654 [telephone] => someOtherTelephoneNumber555 ) Fatal error: Uncaught Error: Undefined class constant 'keys' in /in/KBmA3:9 Stack trace: #0 /in/KBmA3(70): RecursionImplementation::recursionInitiator(Array, 'telephone,login') #1 {main} thrown in /in/KBmA3 on line 9
Process exited with code 255.
Output for 5.3.19 - 5.6.28
Array ( [login] => someUsername [password] => somePassword [card_pin] => card_1234 [pin] => pin_356 [iin] => iin_123 [shipping] => Array ( [value] => Array ( [telephone] => Array ( [inner_value] => telephone_998 ) ) ) [cardnumber] => card_number_876 [customer_id] => customer_654 [telephone] => someOtherTelephoneNumber555 ) Fatal error: Undefined class constant 'keys' in /in/KBmA3 on line 9
Process exited with code 255.