Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- $raw = '{
- "success": [
- {"produto_id": 442},
- {"produto_id": 443},
- {"produto_id": 444},
- {
- "5": {
- "ingredient_id": 5,
- "name": "azeitona"
- },
- "23": {
- "ingredient_id": 23,
- "name": "molho de tomate"
- },
- "27": {
- "ingredient_id": 27,
- "name": "palmito"
- }
- }
- ]
- }';
- $json = json_decode($raw, true);
- $merged = array_map(function($arr) use (&$json) {
- return array_merge($arr, array_shift($json['success']));
- }, $json['success'][3]);
- var_dump($merged);
- // Saída
- /*
- array(3) {
- [5]=>
- array(3) {
- ["ingredient_id"]=>
- int(5)
- ["name"]=>
- string(8) "azeitona"
- ["produto_id"]=>
- int(442)
- }
- [23]=>
- array(3) {
- ["ingredient_id"]=>
- int(23)
- ["name"]=>
- string(15) "molho de tomate"
- ["produto_id"]=>
- int(443)
- }
- [27]=>
- array(3) {
- ["ingredient_id"]=>
- int(27)
- ["name"]=>
- string(7) "palmito"
- ["produto_id"]=>
- int(444)
- }
- }
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement