0

I'm programming in PHP and I have a huge array which contains a lot of ID's. I need all these ID's to be variables so I can use these in another function. I have done a lot of research on loops in PHP but I can find one which "converts" the arrays into variables that I can use in another function. So far I have a foreach loop which processes the whole array and divided into $persons. But when I use $persons in the next function it only uses the last array. My code is as follows:

$retrieved_id_array=explode(",",$retrieved_id_string);

foreach($retrieved_id_array as $persons)
    $retrieved_string=file_get_contents("https://HDXLfansite.com/$persons");

So the problem is how do I make a loop which provides me with several variables I can use in another function? Or should I use another method/code?

2
  • That's what extract() does, but consider if you really need them as variables. You can absolutely pass array elements into functions. callfunction($array['arg1'], $array['arg99']). Much nicer than filling up your namespace with a bunch of one time use variables. Commented Feb 27, 2013 at 2:53
  • Cant you call the function inside the foreach? so you only need the current array only? How many varibles with Ids are you planing to use? I think the correct path is to use the foreach with a call of the funcition inside for each element of the array Commented Feb 27, 2013 at 2:57

1 Answer 1

4

thats because you are overwriting your variable $retrieved_string in loop, you could do:

foreach($retrieved_id_array as $persons) {
   //add it in array
    $retrieved_string[] =file_get_contents("https://HDXLfansite.com/$persons");
}
Sign up to request clarification or add additional context in comments.

Comments

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.