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?

Ifedi Okonkwo
  • 3,406
  • 4
  • 33
  • 45

2 Answers2

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

RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
  • 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? – Ifedi Okonkwo Jul 30 '15 at 16:57
  • [what-is-so-wrong-with-extract](http://stackoverflow.com/questions/829407/what-is-so-wrong-with-extract) – RiggsFolly Jul 30 '15 at 17:07
  • 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. – Ifedi Okonkwo Jul 30 '15 at 22:21
  • 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. – Ifedi Okonkwo Aug 01 '15 at 20:29
  • @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. http://stackoverflow.com/a/3574125/2734189 – Don't Panic Aug 03 '15 at 03:26
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;            
        }
    }
}
Don't Panic
  • 41,125
  • 10
  • 61
  • 80