retesere20

old-approach-array-with-key-php

Jul 18th, 2021
12
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. public function array_with_keys_recursive($array, $keyToRemain){
  3. $new_arr = [];
  4.  
  5. //normal
  6. $keysToRemain= is_array($keyToRemain)?$keyToRemain:[$keyToRemain];
  7. foreach( $array as $key_1=>$val_1 )
  8. {
  9. if (in_array($key_1,$keysToRemain))
  10. $new_arr[$key_1]=$val_1;
  11. }
  12.  
  13. //regex (i.e. *->key1->key2)
  14. $keysToRemain_REGEX = $this->array_only_values_that_contain($keysToRemain,'->');
  15. foreach($keysToRemain_REGEX as $eachRegex)
  16. {
  17. $keysArr= $this->string_to_array($eachRegex, '->');
  18. foreach( $array as $key_1=>$val_1 )
  19. {
  20. if($keysArr[0]==="*" || $keysArr[0]===$key_1){
  21. if (!is_array($val_1))
  22. {
  23. if (empty($keysArr[1]))
  24. $new_arr[$key_1]=$val_1;
  25. }
  26. else{
  27. foreach ($val_1 as $key_2=>$val_2)
  28. {
  29. if($keysArr[1]==="*" || $keysArr[1]===$key_2){
  30. if (!is_array($val_2))
  31. {
  32. if (empty($keysArr[2]))
  33. $new_arr[$key_1][$key_2]=$val_2;
  34. }
  35. else{
  36. foreach ($val_2 as $key_3=>$val_3)
  37. {
  38.  
  39. if ( $keysArr[2]==="*" || $keysArr[2]===$key_3 )
  40. {
  41. if (!is_array($val_3))
  42. {
  43. if (empty($keysArr[3]))
  44. $new_arr[$key_1][$key_2][$key_3]=$val_3;
  45. }
  46. else{
  47. foreach ($val_3 as $key_4=>$val_4)
  48. {
  49. if ( $keysArr[3]==="*" || $keysArr[3]===$key_4 )
  50. {
  51. $new_arr[$key_1][$key_2][$key_3][$key_4]=$val_4;
  52. }
  53. }
  54. }
  55. }
  56. }
  57. }
  58. }
  59. }
  60. }
  61. }
  62. }
  63. }
  64.  
  65. return $new_arr;
  66. }
  67.  
RAW Paste Data