Skip to content

Instantly share code, notes, and snippets.

@rafa-acioly
Last active February 23, 2017 19:46
Show Gist options
  • Save rafa-acioly/454cd927ad3d2bef00fb18492791e3b7 to your computer and use it in GitHub Desktop.
Save rafa-acioly/454cd927ad3d2bef00fb18492791e3b7 to your computer and use it in GitHub Desktop.
<?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)
}
}
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment