1

I feel like I'm struggling to find the answer here because I think I'm missing some key piece of info.

What I am trying to do is run a loop over some data, and then do something different with it based on one of it's values.

So the print_r of the data I am looping over gives me this. This is all good, it has only the data I need, nothing excess.

Array
(
[foodid] => 1
[menuid] => 1789798798
[creatorid] => 1
[foodtype] => hotdog
[frequency] => weekly
[cost] => 20
[chargedate] => 2017-07-14 11:05:18
)

And I want to do SOMETHING to it, depending on the value in frequency. The things I want to do are all stored in a set of identical arrays.

weekly, 2weekly, monthly, daily. eg

$_weekly = array(
"cost" => "2",
"order" => "5",
"years" => "0",
);

$_2weekly = array(
"cost" => "4",
"order" => "10",
"years" => "0",
);

Similar arrays for weekly 2weekly etc.

It seems simple to just use a var like $workingvar in the loop. So when I get there I can just use $workingvar = $_weekly, or $workingvar = $_2weekly.

So, how can I set the contents of $workingvar to be one of those existing arrays? Then I can use the same loop/function on each row in the data, and change the values it pulls in, depending on what that frequency value contains.

Edit added second array example.

3
  • It's unclear what you're asking. Could you please show us the expected result? Commented Jul 16, 2017 at 10:20
  • 1
    Possibly helpful: stackoverflow.com/questions/11419076/… Also: stackoverflow.com/questions/9257505/… Commented Jul 16, 2017 at 10:22
  • @PeterMader I've added a second array example above. I need $workingvar to contain ONE of those arrays depending on what $row->frequency says. Commented Jul 16, 2017 at 10:35

2 Answers 2

2

So you need variable variable, in your case it is:

$arr['frequency'] = 'weekly';
$_weekly = [11,22,33];

$workingvar = ${'_' . $arr['frequency']};
var_dump($workingvar);

But having array with same keys is a more preferred solution (simply because it's more readable and shorter):

$arr['frequency'] = 'weekly';
$arrs = [
    'weekly' => [11,22,33],
];
var_dump($arrs[$arr['frequency']]);
Sign up to request clarification or add additional context in comments.

6 Comments

OK, I get what this is doing. But I'm not sure how it would accept a second value. Edited comment above to show a second set of values. How would I implement many different options in this fashion? Could be 12-15 possible "frequency" choices. Or is @Aurel-Bílý the way to do this?
I don't understand the problem. What will happen if you will have $arr['frequency'] = '2weekly'; instead of $arr['frequency'] = 'weekly';?
I need $workingvar to hold completely different values for weekly vs 2weekly. That's the whole problem I am trying to solve.
And what? What is the exact problem? You have two vars $_weekly, $_2weekly, you have $arr['frequency'] which has a value, what's the problem with $workingvar = ${'_' . $arr['frequency']};?
This was a mistake on my part. I completely overlooked that you were setting the value of $arr['frequency'] to read it. I already have frequency in a variable, and can use that to set $workingvar. Marked as the right answer. This has been a huge help.
|
1

There are many ways to achieve this. If you can guarantee frequency to always have a valid value, then a global look-up array like this could do:

// Define your $_weekly, $_2weekly, etc here

$freqLookup = array(
    "weekly" => $_weekly,
    "2weekly" => $_2weekly,
    // et cetera
  );

Then in your code you simply assign to $workingvar:

$workingvar = $freqLookup[$data["frequency"]];

If you cannot guarantee frequency to always have a valid value, then you can check that the key exists first:

if (!isset($freqLookup[$data["frequency"]])) {
  // Throw an exception or do something to handle the error
}

A possible alternative (but less elegant) solution is a switch, and even less elegant is an if ... else if ... else chain. They could have their merits if you needed to do something very different for each case.

Edit: u_mulder's answer is probably even better IF you can guarantee the sanity of your data. If you are accepting user data, it could be dangerous. Another advantage to a lookup array is that you could map keys to different variables, e.g. "2weekly" => $_biweekly

1 Comment

I'm confident in the data integrity, it's not user input but fed from a form, and then a function runs to create this database entry. It couldn't ever have the wrong thing. And I know enough to know that a giant switch or else if else is NOT going to be a pretty way to handle this at all. Looks like this lookup array might be it.

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.