Reputation: 91
I currently have 2 functions; i need to read one array that's inside one of the functions from the inside of the other function:
function a(){
foreach ($options as $option){ // $options is a variable from function b()
// here goes a really long loop
}
}
function b(){
$options[] = array(
'key1' => 1,
'key2' => 2,
'key3' => 3,
);
function a(); // run function here?
}
My php file is gonna have multiple copies of what we call "function b()". And inside each function b(), i wanna run function a(), but use the array content that's inside the function b().
function a() contains a loop that's always the same but really long, i just want to keep my code as short as possible instead of copying function a() inside each function b().
This is probably really easy but i've been struggling with this issue for a long time now!
Upvotes: 0
Views: 297
Reputation: 219794
Just return the array from b()
and then pass it as a parameter to a()
function a($options){
foreach ($options as $option){
// here goes a really long loop
}
}
function b(){
$options = array(
'key1' => 1,
'key2' => 2,
'key3' => 3,
);
a($options);
}
Upvotes: 3
Reputation: 39522
Could you not simply pass it as a function argument?
function a($options){
foreach ($options as $option){ // $options is a variable from function b()
// here goes a really long loop
}
}
function b(){
a(array(
'key1' => 1,
'key2' => 2,
'key3' => 3,
));
}
Upvotes: 1