0

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]}

4
  • 4
    Don't echo JSON from the functions. They should just return arrays, then you can have another function call them, combine the data, and echo the JSON of that. Commented Sep 23, 2022 at 19:57
  • That makes sense. I have not been able to make a return of the data in arrays though. How would you write that so it can be accessed from one function to another? $array = [ "foo" => "bar", "bar" => "foo", ]; Commented Sep 23, 2022 at 20:31
  • 1
    This is not bash, you don't capture output from functions, you return data. Replace every echo json_encode(...) with return and 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. Commented Sep 23, 2022 at 22:22
  • @Sammitch - want to give credit where credit is due. "echo json_encode(...) with return and move your try/catch outside the function definitions, ideally enclosing the workflow where the functions are called. " this filled in the gap in my understanding enough to work this out on my own. Please repost as an answer if you want the sweet internet points. If not, thank you. Commented Sep 26, 2022 at 17:04

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.