Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Assigning $variables from an Array

Featured Replies

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

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.

$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?

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

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?

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?

If the arrays are set up properly and the site is documented it isn't a problem.

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 ?

  • 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.**

  • 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

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

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.