I have three functions, live_event() - the main function, archived_video() - runs inside live_event() if conditions fail and additional_videos() which should be run in all cases and its results appended to the returned json from both archived_video() and the else clause of live_event(). I want to take the JSON from additonal_videos() and merge it to both live_event() and archived_video() into one JSON object. The ideal output would be:
[{"status":"live_stream","data":[{"uri":"xxx","link":"xxx","title":"xxx".//tons of other data]},{"additional_videos":true,"additional_video_1":"iframe code","additional_video_2":"iframe code","additional_video_3":"iframe code"}]
I cannot get array_merge to work
json_encode(array_merge(json_decode(live_event, true),json_decode(additional_videos(), true)))
And I cannot figure out how to get the functions to return data in the correct format to get array merge to work. Is this possible? How do I need to structure my data so I can access the results of each function inside another function and output one JSON object?
On their own, I can call either of these and they return the appropriate data like so:
function additional_videos() {
//API creds
$body = $response['body']['data'];
try {
$additional_video1 = $body[1]['embed']['html'];
$additional_video2 = $body[2]['embed']['html'];
$additional_video3 = $body[3]['embed']['html'];
header('Content-Type: application/json');
if(empty($additional_video1)) {
echo json_encode([
"additional_videos" => false,
"data" => "Additional video error"
]);
} else {
echo json_encode([
"additional_videos" => true,
"additional_video_1" => $additional_video1,
"additional_video_2" => $additional_video2,
"additional_video_3" => $additional_video3,
]);
}
}
catch(Exception $e){
echo json_encode([
"status" => false,
"message" => $e->getMessage()
]);
}
}
this returns: {"additional_videos":true,"additional_video_1":"iframe code","additional_video_2":"iframe code","additional_video_3":"iframe code"}
function archived_video() {
//API creds
$body = $response['body']['data'];
try {
$archived_video = $body[0]['embed']['html'];
header('Content-Type: application/json');
if(empty($archived_video)) {
echo json_encode([
"status" => "archive_error",
"data" => "Archived video error"
]);
} else {
echo json_encode([
"status" => "archive",
"data" => $archived_video
]);
}
}
catch(Exception $e){
echo json_encode([
"status" => false,
"message" => $e->getMessage()
]);
}
}
This returns: {"status":"archive","data":"iframe code"}
function live_event() {
//AP Creds
$body = $response['body'];
try {
$results = array_filter($body['data'], function($item) {
if(!isset($item['metadata']['connections']['live_video']['status']))
return false;
return $item['metadata']['connections']['live_video']['status'] == "streaming";
});
header('Content-Type: application/json');
if(empty($results)) {
archived_video();
return;
} else {
echo json_encode([
"status" => "live_stream",
"data" => array_values($results)
]);
}
}
catch(Exception $e){
echo json_encode([
"status" => false,
"message" => $e->getMessage()
]);
}
}
This returns:{"status":"live_stream","data":[{"uri":"xxx","link":"xxx","title":"xxx".//tons of other data]}
echo json_encode(...)withreturnand move your try/catch outside the function definitions, ideally enclosing the workflow where the functions are called. Then you can actually work with the function results. There's also no "accessed from one function to another" there is only function returns and function parameters.