Advertisement
Guest User

Untitled

a guest
Feb 23rd, 2017
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.98 KB | None | 0 0
  1. <?php
  2.  
  3. $raw = '{
  4. "success": [
  5. {"produto_id": 442},
  6. {"produto_id": 443},
  7. {"produto_id": 444},
  8. {
  9. "5": {
  10. "ingredient_id": 5,
  11. "name": "azeitona"
  12.  
  13. },
  14. "23": {
  15. "ingredient_id": 23,
  16. "name": "molho de tomate"
  17.  
  18. },
  19. "27": {
  20. "ingredient_id": 27,
  21. "name": "palmito"
  22.  
  23. }
  24. }
  25. ]
  26. }';
  27.  
  28.  
  29. $json = json_decode($raw, true);
  30.  
  31. $merged = array_map(function($arr) use (&$json) {
  32. return array_merge($arr, array_shift($json['success']));
  33. }, $json['success'][3]);
  34.  
  35. var_dump($merged);
  36.  
  37.  
  38. // Saída
  39. /*
  40. array(3) {
  41. [5]=>
  42. array(3) {
  43. ["ingredient_id"]=>
  44. int(5)
  45. ["name"]=>
  46. string(8) "azeitona"
  47. ["produto_id"]=>
  48. int(442)
  49. }
  50. [23]=>
  51. array(3) {
  52. ["ingredient_id"]=>
  53. int(23)
  54. ["name"]=>
  55. string(15) "molho de tomate"
  56. ["produto_id"]=>
  57. int(443)
  58. }
  59. [27]=>
  60. array(3) {
  61. ["ingredient_id"]=>
  62. int(27)
  63. ["name"]=>
  64. string(7) "palmito"
  65. ["produto_id"]=>
  66. int(444)
  67. }
  68. }
  69. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement