2

I'm pretty new and need guidance on how to approach a simple problem.

I'm building an app in PHP to allow different views of Fantasy Premier League (FPL) team data. I'm pulling data from an API (JSON array), looping through it, and storing it in a db. One of the API calls needs a team_id to pull that team's respective players.

My initial thinking is to write a function that takes the team_id as an argument, then parses the data to insert into the db. But how would I pass each of 12 different team_ids to the function to be processed without repeating the function 12 times for each team_id?

So, I might have:

$team_id_1 = 1;
$team_id_2 = 2;
$team_id_3 = 3;
etc...

Then my function might be:

function insert_team_data($team_id) {

$fplUrl = 'https://fantasy.premierleague.com/api/entry/' . $team_id . '/event/29/picks/';

foreach ($team_data as $key => $value) {
    # code...
}

$sql = "INSERT INTO ..."

}

Is it possible to construct this in a way where each team_id gets passed to the function iteratively in a single process (i.e. without repeating the function code for each team_id)? Would I create an array of the team_ids and have the function loop through each one, then loop through each resulting team's data?

1
  • 1
    change $team_id_1 etc into array like $team_ids = [1,2,3] and then loop over it, also layer a bit of logic around or in insert_team_data so your not repeating calls to the API or dupe rows, you can SELECT before insert etc, if you need it updated, store a datetime which you can compare from the select result Commented Apr 5, 2020 at 2:52

1 Answer 1

2

Yes, you can do either: use an array or a variadic function.

The way you're thinking of doing it is what's called a variadic function or variable-length function.

It can be achieved through either the use of func_get_args() or the splat operator, which handles argument packing/unpacking.

Here's an example.

function insert_team_data(... $team_ids) {

    // $team_ids will appear as an array in your function    
    foreach ($team_ids as $team_id) {
        $fplUrl = "https://fantasy.premierleague.com/api/entry/$team_id/event/29/picks/";
        $sql = "INSERT INTO ..."
        // rest of code...
    }


}

You can now call the function like this: insert_team_data(1, 2, 3, 4, 5)

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you, Sherif. I'll try it out in short order.
So, this solution helped me solve something unrelated, and the splat operator is a valuable piece to keep in the arsenal. The API I'm accessing didn't seem to like that I was trying to make multiple calls. I know it's because I haven't set things up properly, just like the other commenter suggested I avoid. I ended up writing a longish function that I repeat for each $team_id, so the script is clunky but works for now. I'll rewrite it down the line. Thanks for everyone's help.

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.