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.

Timps
  • 58
  • 8
  • It's unclear what you're asking. Could you please show us the expected result? – PeterMader Jul 16 '17 at 10:20
  • 1
    Possibly helpful: https://stackoverflow.com/questions/11419076/php-using-variable-value-as-variable-name-curious-about-syntax-issue Also: https://stackoverflow.com/questions/9257505/dynamic-variable-names-in-php – Fred Gandt Jul 16 '17 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. – Timps Jul 16 '17 at 10:35

2 Answers2

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']]);
u_mulder
  • 54,101
  • 5
  • 48
  • 64
  • 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? – Timps Jul 16 '17 at 10:40
  • I don't understand the problem. What will happen if you will have `$arr['frequency'] = '2weekly';` instead of `$arr['frequency'] = 'weekly';`? – u_mulder Jul 16 '17 at 10:48
  • I need $workingvar to hold completely different values for weekly vs 2weekly. That's the whole problem I am trying to solve. – Timps Jul 16 '17 at 10:58
  • 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']};`? – u_mulder Jul 16 '17 at 11:07
  • 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. – Timps Jul 16 '17 at 11:26
  • Glad to help, btw you can remove unnecessary comments) – u_mulder Jul 16 '17 at 11:28
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

Aurel Bílý
  • 7,068
  • 1
  • 21
  • 34
  • 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. – Timps Jul 16 '17 at 10:42