I am getting json data from an api endpoint, I would like to add a key value to an array that I get. This is my function:
$magazines = Magazine::all();
foreach ($magazines as $magazine) {
$result = file_get_contents('http://customer.pages.com/?customer=' . $magazine->visio_link_prefix . '&action=latest');
$issues[] = json_decode($result, true);
}
foreach ($issues as $issue) {
Issue::create([
''
'title' => $issue['papers'][0]['title'],
'date' => $issue['papers'][0]['date'],
'foldername' => $issue['papers'][0]['foldername'],
'thumb' => $issue['papers'][0]['thumb'],
'thumbmedium' => $issue['papers'][0]['thumbmedium'],
]);
}
The array that I get from the endpoint looks like this:
array:24 [▼
0 => array:1 [▼
"papers" => array:1 [▼
0 => array:11 [▼
"title" => "News- 2014-10-22"
"date" => "2014-10-22"
"expires" => ""
"catalog" => 24
"foldername" => "News"
"folder" => 4965
"pages" => 132
"sectionstarts" => "1"
"sectioncount" => 1
"thumb" => "www.customer.pages.com/news/24/teasers/small.jpg"
"thumb_medium" => "www.customer.pages.com/news/24/teasers/medium.jpg"
]
]
]
1 => array:1 [▶]
2 => array:1 [▶]
3 => array:1 [▶]
4 => array:1 [▶]
5 => array:1 [▶]
6 => array:1 [▶]
7 => array:1 [▶]
8 => array:1 [▶]
9 => array:1 [▶]
10 => array:1 [▶]
11 => array:1 [▶]
12 => array:1 [▶]
13 => array:1 [▶]
14 => array:1 [▶]
15 => array:1 [▶]
16 => array:1 [▶]
17 => array:1 [▶]
18 => array:1 [▶]
19 => array:1 [▶]
20 => array:1 [▶]
21 => array:1 [▶]
22 => array:1 [▶]
23 => array:1 [▶]
]
So, in my foreach loop I would like to add a key value pair 'magazineId' => $magazine->id to each of the above 'papers' arrays. So that later I can use $issue['papers'][0]['magazineId'] to get the value of the $magazine->id. Not sure how to do that?
$issues. Dump of the endpoint would be something like this: array (size=1) 'papers' => array (size=1) 0 => array (size=11) 'title' => string 'Mamma- 2014-10-22' (length=17) 'date' => string '2014-10-22' (length=10) 'expires' => string '' (length=0) 'catalog' => int 24 ...