I've got 4 PHP variables, and I have 1 Javascript variable with the name of one of those 4 PHP variables, how can reference it?
$p1 = ...
$p2 = ...
$p3 = ...
$p4 = ...
<script>
var myoption = 'p1';
var myarray = <?= $p1 ?>;
console.log(myarray);
</script>
In this case instead of setting myarray to $p1, I want to do it with myoption, once myoption can be p2 or p3
You can create a PHP array, json_encode it into your Javascript, like this:
$p1 = ...
$p2 = ...
$p3 = ...
$p4 = ...
$myPHPArray = [
'p1' => $p1,
'p2' => $p2,
'p3' => $p3,
'p4' => $p4
];
<script>
var myoption = 'p1';
var myarray = <?php echo json_encode($myPHPArray) ?>;
console.log(myarray[myoption]);
</script>