Advertisement
Guest User

Untitled

a guest
Apr 21st, 2019
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.83 KB | None | 0 0
  1. <?php
  2.  
  3. $data = [
  4. 'content/price/value' => 10.01,
  5. 'content/price/currency' => 'USD',
  6. 'content/shipping/free' => false,
  7. 'content/descrtiption' => 'hallo meine name ist dominik stapf',
  8. 'content/erp_name/0/locale' => 'en_US',
  9. 'content/erp_name/0/scope' => null,
  10. 'content/erp_name/0/data' => 'Jack',
  11. 'content/erp_name/1/locale' => 'de_DE',
  12. 'content/erp_name/1/scope' => 'NULL',
  13. 'content/erp_name/1/data' => 'Jacke'
  14. ];
  15.  
  16. $result = [];
  17.  
  18. foreach ($data as $structure => $value) {
  19. $reference = &$result;
  20.  
  21. $nodes = explode('/', $structure);
  22.  
  23. foreach ($nodes as $node) {
  24. if (!isset($reference[$node])) {
  25. $reference[$node] = [];
  26. }
  27. $reference = &$reference[$node];
  28. }
  29.  
  30. $reference = $value === 'NULL' ? null : $value;
  31. }
  32.  
  33. echo json_encode($result, JSON_PRETTY_PRINT);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement