3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?php class Recursion { public static function recursionLevelOne($information, $keylist) { $global_array = array(); $keys = explode($keylist, ","); foreach($keys as $individual_key) $global_array = self::recursionLevelTwo($information, $individual_key); return $global_array; } private static function recursionLevelTwo($array, $key) { if(count($array) == 0) return; $global_array = array(); foreach($array as $k => $v) { if($k == $key) if($array[$k] != self::$asterisk) $array[$k] = self::$asterisk; else { if(is_array($v)) { $temp_array = self::recursionLevelTwo($v, $key); array_push($global_array, $temp_array); } } } return $global_array; } private static $asterisk = "**"; } $information = array("login" => "someUsername", "password" => "somePassword", "card_pin"=>"card_1234", "pin"=>"pin_356", "iin" => "iin_123", "shipping" => array("telephone" => "telephone_998"), "cardnumber" => "card_number_876", "customer_id" => "customer_654", "telephone" => "someOtherTelephoneNumber555"); $list = "login,customer_id"; print_r($information); echo PHP.EOL; echo PHP.EOL; $global_array = Recursion::recursionLevelOne($information, $list); print_r($global_array);
based on sUWnd
Output for 7.2.0
Array ( [login] => someUsername [password] => somePassword [card_pin] => card_1234 [pin] => pin_356 [iin] => iin_123 [shipping] => Array ( [telephone] => telephone_998 ) [cardnumber] => card_number_876 [customer_id] => customer_654 [telephone] => someOtherTelephoneNumber555 ) Warning: Use of undefined constant PHP - assumed 'PHP' (this will throw an Error in a future version of PHP) in /in/RLUaq on line 57 Warning: Use of undefined constant EOL - assumed 'EOL' (this will throw an Error in a future version of PHP) in /in/RLUaq on line 57 PHPEOL Warning: Use of undefined constant PHP - assumed 'PHP' (this will throw an Error in a future version of PHP) in /in/RLUaq on line 58 Warning: Use of undefined constant EOL - assumed 'EOL' (this will throw an Error in a future version of PHP) in /in/RLUaq on line 58 PHPEOLArray ( )
Output for 5.3.19 - 7.1.10
Array ( [login] => someUsername [password] => somePassword [card_pin] => card_1234 [pin] => pin_356 [iin] => iin_123 [shipping] => Array ( [telephone] => telephone_998 ) [cardnumber] => card_number_876 [customer_id] => customer_654 [telephone] => someOtherTelephoneNumber555 ) Notice: Use of undefined constant PHP - assumed 'PHP' in /in/RLUaq on line 57 Notice: Use of undefined constant EOL - assumed 'EOL' in /in/RLUaq on line 57 PHPEOL Notice: Use of undefined constant PHP - assumed 'PHP' in /in/RLUaq on line 58 Notice: Use of undefined constant EOL - assumed 'EOL' in /in/RLUaq on line 58 PHPEOLArray ( )