2

Is there a better/shorter way to do this?

Each variable is a MySQL table value that is called. I have a main table and an override table, so call the first table, extract the resulting array, then call the override table and extract those results to override the first extract.

if (isset($Price1)){
    $AllPrices[] = $Price1;}
if (isset($Price2)){
    $AllPrices[] = $Price2;}
if (isset($Price3)){
    $AllPrices[] = $Price3;}    
if (isset($Price4)){
    $AllPrices[] = $Price4;}
if (isset($SPrice1)){
    $AllPrices[] = $SPrice1;}
if (isset($SPrice2)){
    $AllPrices[] = $SPrice2;}
if (isset($SPrice3)){
    $AllPrices[] = $SPrice3;}   
5
  • 1
    "All variables are pulling from MySQL" what does this mean? That is, how are these variables being set specifically? Commented Mar 1, 2013 at 21:23
  • Not really, it looks like you need to check each individual variable, which you're doing. Unless those vars were originally in an array or something... Commented Mar 1, 2013 at 21:23
  • 2
    can you show us the full code (the part that pulls from the DB and loops for example)? Commented Mar 1, 2013 at 21:23
  • 1
    There is a way using variable variables, but I don't recommend it. Best would be to originate all those vars like $Price1 as array keys rather than global vars. Commented Mar 1, 2013 at 21:26
  • Each variable is a table value that is called. I have a main table and an override table, so call the first able, extract, the values, then call the override and extract those values to override. Then I run the code above. Commented Mar 1, 2013 at 21:34

3 Answers 3

4

I've done things like this in the past, but I wouldn't recommend it:

$variables = array("Price1", "Price2", "Price3", "Price4", "SPrice1", "SPrice2", "SPrice3");

$AllPrices = array();

foreach ($variables as $variable)
{
    if (isset($$variable))
        $AllPrices[] = $$variable;
}

See here for more information on how the $$ syntax works in PHP.

But I do agree with the comments to your post... You really should take another look at how you're getting this data. This solution is not ideal in the least.

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

2 Comments

I think it is a neat solution for the information you got from the question :-)
Doesn't work for what I am doing (see my last comment), but nice solution.
0

You can also do like this:

for($i = 1; $i < 4; $i++){
    $current = ${'AllPrices'.$i};
    isset($current) && array_push($AllPrices, $current);
}

As also posted in above answer, using array of variable names is more efficient.

Comments

0

This structure is not recommended.

Instead of using $price1, $price2, ... use an array - $price[1], $price[2], ... (Do the same for $SPrice).

Then you could use a simple array_merge:

$AllPrices = array_merge($price, $SPrice);

Comments

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.