1

I have this function:

function extractAvailable($assoc1, $assoc2){
    if($assoc1) extract($assoc1);
    else extract($assoc2);
}

What I expect is to call this function later in the global scope, and have my variables available, like so:

$arr1 = [];
$arr2 = ['one'=>1, 'two'=>'SecondItem'];
extractAvailable($arr1, $arr2);

With the call on extractAvailable(), I need to have the varialbes $one and $two available in the current scope. Obviously, I've got something wrongly figured concerning variable scope use here, 'cause it isn't working. When I try to use the variable, what I get instead is Notice: Undefined variable: one.

How do I get this to work?

2
  • Yep, that's a scope issue. extract can only expand to the current scope. Commented Jul 30, 2015 at 16:24
  • Well, no workaround? ;( Commented Jul 30, 2015 at 16:29

2 Answers 2

2

You could add the new data to the $GLOBALS array which would have the effect of making them available in other scopes.

function extractAvailable($assoc1, $assoc2){
    if($assoc1) {
        foreach ($assoc1 as $key => $value) {
            $GLOBALS[$key] = $value;            
        }
    } else {
        foreach ($assoc2 as $key => $value) {
            $GLOBALS[$key] = $value;            
        }
    }
}

But I have to wonder why you need to extract anything from a perfectly good array and place the exact same data into scalar variables. All this does is double your memory requirement for no benefit whatsoever

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

5 Comments

I'd ask: why did they "invent" extract() is it was so useless? The main reason I extract is, minimize the bulk of code I'd otherwise have to type. Think of $big_array['result']['firstkey']. When $big_array['result'] is extracted, you can then happily use all of $firstkey..$hundredthkey. In any case, I do take note of your warning about memory footprint: how bad is this downside?
Thanks for the link, it made a rather interesting read! In my case, I've made much use of extract(), but generally for data on the way OUT rather than IN. For instance, it has come in quite handy for exposing variables onto form templates.
I'm thankful for your useful input. I went ahead and accepted Don't Panic's answer since it popped in first. I realize that your answer would work just as well, and does indeed have the advantage of brevity.
@IfediOkonkwo Check out this answer and the linked post for some discussion of global and $GLOBALS. I saw what you said about my answer being first, but I think this one is better. I had actually forgotten about $GLOBALS at the time I answered, because I don't usually use globals, or I would have suggested the same. stackoverflow.com/a/3574125/2734189
1

If you want them to be available in the global scope, you can use variable variables instead of extract, and specify them as global.

function extractAvailable($assoc1, $assoc2){
    if($assoc1) {
        foreach ($assoc1 as $key => $value) {
            global $$key;
            $$key = $value;            
        }
    } else {
        foreach ($assoc2 as $key => $value) {
            global $$key;
            $$key = $value;            
        }
    }
}

1 Comment

Hmm...interesting. I'll check this out and give feedback. Thanks.

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.