July 3, 200818 yr I have an Array that holds between 2 and 5 pieces of data. How would I extract these pieces of data so they are stored in separate $variables? It needs to be done with a loop statement, I think.. because the amount of data in the array will change based on User input. I'd like to be able to use the variables in my MySQL queries. Thankyou! Jarrad
July 3, 200818 yr You have to use a foreach statement i.e: foreach ($arrayname as $variable){ echo $variable;} although that simply prints the variables out, I'm not sure how you'd store that as a discrete set of variables.
July 3, 200818 yr $array = array('item1' => 'this is numero uno', 'item2' => 'this is numero duo?'); extract($array); print $item1; // prints: this is numero uno print $item2; // prints: this is numero duo?
July 3, 200818 yr Looping through an array like this should work: <?php //add some variable names to an array of variables $arrVariables[] = "strHello"; $arrVariables[] = "strTest"; for($iIndex = 0; $iIndex<count($arrVariables); $iIndex++) { //create the variables ${$arrVariables[$iIndex]} = "This variable is called ".$arrVariables[$iIndex]; } echo $strHello; echo $strTest; ?> The output should be: This variable is called strHello This variable is called strTest
July 3, 200818 yr Looping through an array like this should work: <?php //add some variable names to an array of variables $arrVariables[] = "strHello"; $arrVariables[] = "strTest"; for($iIndex = 0; $iIndex<count($arrVariables); $iIndex++) { //create the variables ${$arrVariables[$iIndex]} = "This variable is called ".$arrVariables[$iIndex]; } echo $strHello; echo $strTest; ?> The output should be: This variable is called strHello This variable is called strTest Why on earth would you do that when you can do it in 1 line?
July 3, 200818 yr Why on earth would you do that when you can do it in 1 line? There are several reasons the main one being for clarity. The exctract funtion lends itself well to poor coding practice. When working on large projects with multiple developers you are likely to find yourself wondering where a variable has come from if 'extract' has been used. For example: $arrUsers = $this->getUsers(); extract($arrUsers); If ($admin) $this->displayAdminPanel; Where did $admin come from?
July 3, 200818 yr As a side note, it's a good practice to count the array outside the loop, so instead of this: for($iIndex = 0; $iIndex<count($arrVariables); $iIndex++) { .... } you should always use this: //count the array only once $nr_of_elements = count($arrVariables); //for statement doesn't execute count every time this way for ($i = 0; $i < $nr_of_elements; $i++) { ..... } As for your question ... are you trying to get the variables from the $_POST array ?
July 3, 200818 yr Author Thanks for the input guys. It looks like I have a few ways to have a play with. Just as a bit of background info: The array is made from a User selecting 1 to 5 choices from a Form Selection Box, and once the Users selections are taken from the array and passed into individual variables, they will be used in SQL Queries to display customized results. I'll let ya's know how I get on with it. Thanks again! **Mihai - They did come from a $_POST array, but I passed that into a single PHP $variable.**
July 4, 200818 yr Author Thanks for the help everyone. I've now managed to write my very first (working) Webpage-based, User-customizable SQL Query that returns results! hmm, now for the tough stuff... LoL Jarrad
July 5, 200818 yr You should be very careful while handling user input ... here's a warning for the extract function: Do not use extract() on untrusted data, like user-input ($_GET, ...). If you do, for example, if you want to run old code that relies on register_globals temporarily, make sure you use one of the non-overwriting extract_type values such as EXTR_SKIP and be aware that you should extract in the same order that's defined in variables_order within the php.ini.
Create an account or sign in to comment