0

I have been looking for an answer, but maybe because I am searching the wrong terms or concepts - I did not yet find any lead on how to perform operations on extracted arguments .

I have an array, which is dynamic ( I can not predict how many $vars or of what name or type )

global $o99_option;
extract( $o99_option );

so now, theoretically, I can have any number of $vars with any possible name..

example :

$first_var = 'first-var';
$my_second_var = 'another';
$enigma = NULL;
$answer = '42';

my question is - how can I perform operations on the result of extract ( after extraction ) in a manner that will effect all the created $vars ?

for example, let´s say I want to trim all of those variables - How do I achieve that ?

( that , In my twisted mind, should have the same effect as

foreach (extract( $o99_option ) as $single_var ){ do_something($single_var); }

)

Edit I : So, is there a way to perform operations on all extracted elements ? ( assuming I DO need to extract them ..) Or should I always do that BEFORE the extraction ( for example with the help of array_map() ) - or, should I , like people here suggested just forget that extract ever existed never ever use it..

Edit II :

Sure, like many here said , I can also do

$first_var = $o99_option['first-var'];
$my_second_var = $o99_option['my_second_var'];
$enigma = $o99_option[enigma];
$answer = $o99_option['answer'];

but

1) it seems a little absurd doing that for 100+ variables, and 2 ) what to do with the ones that I need to use their value as name ?

right now, for example, I use

${$title_field} = $formdata[$post_title_field];

where I know that $title_field exists as $o99_option['title_field'], and therefore exists as a variable after extract .. Again, I might be approaching that all wrong, but up until now the script works great , and actually , the first comment by @Mark Baker ( using array_map() ) might be the best option when doing so BEFORE the extract ...

Obmerk Kronen
  • 15,619
  • 16
  • 66
  • 105
  • 3
    Don't extract; just use the array itself in your foreach().... or simply use `array_map('trim', $o99_options);` - see [docs](http://www.php.net/manual/en/function.array-map.php) – Mark Baker Jul 14 '14 at 14:08
  • 1
    `global` *and* `extract` together as the only code shown... Sorry, but you're pretty much using all the anti-patterns there are at once. And see what problems it brings you. – deceze Jul 14 '14 at 14:10
  • @MarkBaker But I do need to extract and use those $vars . the `trim` part was only an example, it can be any number of things . Your suggestion is to perform those operations BEFORE extract ? – Obmerk Kronen Jul 14 '14 at 14:10
  • __Why__ do you need to extract? Why can't you access the values directly in the array? My suggestion is that `extract` is not necessary, and causes maximum headache for no benefit – Mark Baker Jul 14 '14 at 14:11
  • 1
    If you extract, you say yourself that you don't know what the variable will be called, so how can you use it? – Mark Baker Jul 14 '14 at 14:13
  • @MarkBaker - I need to extract them because later in the script I am using ( most of ) them - at least the ones that I know the type .. The others are still unknown but I do use them with variable variables .. – Obmerk Kronen Jul 14 '14 at 14:14
  • anyhow , the `array_map()` option is valid to perform before the extraction - and then I can extract normally. It will work with any kind of function actually - I just thought there is some other way to handle extracted elements that I do not know of .. ( like a flag or something ) – Obmerk Kronen Jul 14 '14 at 14:17
  • If you have an unknown number of items, an array is always the better option. Precisely because they're all bundled together in the array and you can enumerate them all without having to know how many there are or what they're called. When extracting them all into individual variables, you're losing exactly that ability, which means the rest of your code practically becomes entirely indeterminate. Just work with those values in the array, you don't need them as individual variables. Really. – deceze Jul 14 '14 at 14:20
  • 1
    `variable variables`! Now you're compounding the problem.... extracting from an array is never a good idea, and never necessary – Mark Baker Jul 14 '14 at 14:23
  • @deceze - I am trying to understand here - But I really can not grasp why people assume that I can just use it as an array - how can I later create variables that will have the same name as the array elements (and their values ) in an efficient way ? – Obmerk Kronen Jul 14 '14 at 14:26
  • @MarkBaker - Ok, So I will not extract . So the simple answer to my question would be "You can not perform operations after extract " and also - " You should NEVER use extract " . So please add that as an answer - And I will accept it. ( Note that the aim of this script, and this question - is learning. and if that means learning `NEVER` to use extract - then All good the same .. – Obmerk Kronen Jul 14 '14 at 14:27
  • Please clarify why you ***need*** to have those *values* as individual variables. Why is it impossible to do `$foo['bar']`? Why do you ***need*** `$bar`? – deceze Jul 14 '14 at 14:33
  • @deceze - Because on some of them , i do not know that they will be called `bar`? - But hey - Like said before - maybe the right answer is that I never really need to extract . If someone will post it as an answer - I will be more than happy to accept ( and comply ) – Obmerk Kronen Jul 14 '14 at 17:18
  • 1
    If you don't know the names of some keys, extracting them gives you *even less* of a chance of working with them... – deceze Jul 14 '14 at 17:19

3 Answers3

2

You must not extract the variables, you need to keep them in the $o99_option array otherwise it's impossible to determine the number of elements.

foreach ($o99_option as $variable) {
    // Do something with the variable
}
epicdev
  • 406
  • 3
  • 6
1

Imagine you're sitting in one of those kids ball pits. This is your variable scope. You have all sorts of variables floating around in this scope. Now you get handed a box full of even more colourful balls. Those balls are important balls, they're more interesting to you than all the other balls around you.

What you're doing with extract is you're turning the box upside down and empty all those balls into the ball pit and mix them with all the other balls. And you didn't even really know what balls were in that box exactly, but you emptied the box anyway. Well, good luck finding those balls again now and doing anything with them.

Keep the balls in the box! While they're in the box, you can inspect them one by one. You can look at each one in turn and do something with it. You can carry them all around inside that box together. Don't extract!

If you know what you're looking for in that box:

echo $o99_option['first-var'];

Simple. Just take that ball directly and do whatever you want with it.
If you want to check whether a specific ball is in the box:

if (isset($o99_option['first-var']))

If you want to look through all balls and do something with each of them:

foreach ($o99_option as $name => $ball) {
    echo "$name: $ball";
}

ball pit

deceze
  • 510,633
  • 85
  • 743
  • 889
  • ok, I always accept wiser wisdom, but then - when SHOULD I use extract ? never ?? Even in my current scope, I have about 300 known name variables.. should I just took all of them and create new variable for them ? e.g. `$my_variable_with_make_sense_name = $o99_option['first-var']` ? BTW - since I am in China now - I can not really see an Image - But I am sure it is cool :-) – Obmerk Kronen Jul 15 '14 at 11:15
  • I basically never use `extract`, I have a hard time finding a useful case for it. Arguably you can implement a simple templating system with it: `function render($file, array $vars) { extract($vars); include $file; }`, but that's about the only thing I can think of. Your array keys should already have useful names, so I'm not sure why you insist on taking anything out of it in the first place. :) – deceze Jul 15 '14 at 11:47
0

If you want to do something to all the variables:

foreach ($o99_option as &$variable) {
    $variable = strtoupper($variable); //for example
}

Then you should work with each element individually. Don't extract.

Community
  • 1
  • 1
miken32
  • 42,008
  • 16
  • 111
  • 154