fork download
  1. <?php
  2.  
  3. $plan = [[
  4. 'plan' => null,
  5. 'image_upload_cnt' => '0',
  6. 'emojis_cnt' => '0',
  7. 'stickers_cnt' => '0',
  8. 'profile_gif' => '0',
  9. 'cover_gif' => '0',
  10. 'avatar_effect' => '0',
  11. 'background_effect' => '0',
  12. 'credits_to_unlock' => '0',
  13. ],[
  14. 'plan' => null,
  15. 'image_upload_cnt' => '0',
  16. 'emojis_cnt' => '0',
  17. 'stickers_cnt' => '0',
  18. 'profile_gif' => '0',
  19. 'cover_gif' => '0',
  20. 'avatar_effect' => '0',
  21. 'background_effect' => '0',
  22. 'credits_to_unlock' => '0',
  23. ]];
  24. function boostValuesToBool($rows)
  25. {
  26. $toBool = static function ($v): bool {
  27. if ($v === 1 || $v === '1' || $v === true) return true;
  28. if ($v === 0 || $v === '0' || $v === false) return false;
  29. return false;
  30. };
  31.  
  32. $apply = static function (array $r) use ($toBool): array {
  33. $fields = ['profile_gif', 'cover_gif', 'avatar_effect', 'background_effect'];
  34.  
  35. foreach ($fields as $f) {
  36. if (array_key_exists($f, $r)) {
  37. $r[$f] = $toBool($r[$f]);
  38. }
  39. }
  40.  
  41. return $r;
  42. };
  43.  
  44. // If it's a single associative array (a "row"), normalize and return it
  45. if (is_array($rows) && $rows !== [] && array_keys($rows) !== range(0, count($rows) - 1)) {
  46. return $apply($rows);
  47. }
  48.  
  49. // Otherwise assume it's a list of rows
  50. foreach ($rows as &$r) {
  51. if (is_array($r)) {
  52. $r = $apply($r);
  53. }
  54. }
  55. unset($r);
  56.  
  57. return $rows;
  58. }
  59.  
  60. $rr= boostValuesToBool($plan);
  61. var_export($rr);
Success #stdin #stdout 0.03s 25508KB
stdin
Standard input is empty
stdout
array (
  0 => 
  array (
    'plan' => NULL,
    'image_upload_cnt' => '0',
    'emojis_cnt' => '0',
    'stickers_cnt' => '0',
    'profile_gif' => false,
    'cover_gif' => false,
    'avatar_effect' => false,
    'background_effect' => false,
    'credits_to_unlock' => '0',
  ),
  1 => 
  array (
    'plan' => NULL,
    'image_upload_cnt' => '0',
    'emojis_cnt' => '0',
    'stickers_cnt' => '0',
    'profile_gif' => false,
    'cover_gif' => false,
    'avatar_effect' => false,
    'background_effect' => false,
    'credits_to_unlock' => '0',
  ),
)