Advertisement
ryanharne

PHP array_dot() - Flatten multidimensional array

Sep 3rd, 2020
1,841
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.09 KB | None | 0 0
  1. <?php
  2.  
  3. $json = '{"id":"a9697c47-dcc6-46ad-8778-000000000000","result":"A","statedIp":"10.8.1.1","trackingNumber":202020202020202020,"details":{"device":{"alias":101010101010101010,"blackboxMetadata":{"age":3543856,"timestamp":"2020-07-24T04:45:18Z"},"browser":{"cookiesEnabled":true,"configuredLanguage":"EN-US,EN;Q=0.9,MS;Q=0.8","language":"EN-US","type":"CHROME","timezone":"-480","version":"83.0.4103.116"},"firstSeen":"2020-06-30T13:12:04.240Z","isNew":false,"os":"WINDOWS NT 10.0","screen":"1440X2560","type":"WINDOWS"},"statedIp":{"address":"10.8.10.00","source":"subscriber"},"realIp":{"address":"118.100.10.10","isp":"TM NET  INTERNET SERVICE PROVIDER","ipLocation":{"city":"KUALA LUMPUR","country":"MALAYSIA","countryCode":"MY","latitude":3.656210,"longitude":125.68690,"region":"KUALA LUMPUR"},"parentOrganization":"TMNST","source":"nandos"},"ruleResults":{"score":0,"rulesMatched":0}}}';
  4. $json = json_decode($json, true);
  5.  
  6. print_r(array_dot($json));
  7.  
  8. /**
  9.  * Flatten multidimensional array into single array, by dot all keys to it's value.
  10.  * Inspired by Laravel.
  11.  *
  12.  * @version PHP 5.3 Safe.
  13.  *
  14.  * @param array  $array
  15.  * @param string $prefix
  16.  * @param string $glue
  17.  *
  18.  * @return array{string: mixed}
  19.  */
  20. function array_dot($array, $prefix = '', $glue = '.')
  21. {
  22.     $result = array();
  23.  
  24.     foreach ($array as $key => $value) {
  25.         if( is_array($value) ) {
  26.             $result = $result + array_dot($value, $prefix . $key . $glue);
  27.         }
  28.         else {
  29.             $result[$prefix . $key] = $value;
  30.         }
  31.     }
  32.  
  33.     return $result;
  34. }
  35.  
  36. /*
  37. Array
  38. (
  39.     [id] => a9697c47-dcc6-46ad-8778-000000000000
  40.     [result] => A
  41.     [statedIp] => 10.8.1.1
  42.     [trackingNumber] => 202020202020202020
  43.     [details.device.alias] => 101010101010101010
  44.     [details.device.blackboxMetadata.age] => 3543856
  45.     [details.device.blackboxMetadata.timestamp] => 2020-07-24T04:45:18Z
  46.     [details.device.browser.cookiesEnabled] => 1
  47.     [details.device.browser.configuredLanguage] => EN-US,EN;Q=0.9,MS;Q=0.8
  48.     [details.device.browser.language] => EN-US
  49.     [details.device.browser.type] => CHROME
  50.     [details.device.browser.timezone] => -480
  51.     [details.device.browser.version] => 83.0.4103.116
  52.     [details.device.firstSeen] => 2020-06-30T13:12:04.240Z
  53.     [details.device.isNew] =>
  54.     [details.device.os] => WINDOWS NT 10.0
  55.     [details.device.screen] => 1440X2560
  56.     [details.device.type] => WINDOWS
  57.     [details.statedIp.address] => 10.8.10.00
  58.     [details.statedIp.source] => subscriber
  59.     [details.realIp.address] => 118.100.10.10
  60.     [details.realIp.isp] => TM NET  INTERNET SERVICE PROVIDER
  61.     [details.realIp.ipLocation.city] => KUALA LUMPUR
  62.     [details.realIp.ipLocation.country] => MALAYSIA
  63.     [details.realIp.ipLocation.countryCode] => MY
  64.     [details.realIp.ipLocation.latitude] => 3.65621
  65.     [details.realIp.ipLocation.longitude] => 125.6869
  66.     [details.realIp.ipLocation.region] => KUALA LUMPUR
  67.     [details.realIp.parentOrganization] => TMNST
  68.     [details.realIp.source] => nandos
  69.     [details.ruleResults.score] => 0
  70.     [details.ruleResults.rulesMatched] => 0
  71. )
  72.  */
  73.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement