-1

I have an array

$locations = [ 'name1', 'name2', 'name3', 'name4' ]

Now I want to create variables say $location1, $location2,...

which will hold value of name1, name2,.... respectively ( i.e., $location1 = 'name1', $location2 = 'name2',..... and so on

The array gets updated whenever admin adds new location so another variable must be created automatically to hold the name of new location from array.

Can it be done using php. Reason for this is I will need to access those variables and assign their values to javascript variable for displaying their name in map.

Any help would be appreciated.

maverick
  • 141
  • 1
  • 13
  • Not sure why you want to do this, it's exactly what arrays are for, BUT (and I don't recommend this) - https://stackoverflow.com/questions/25593055/variable-variables-in-php-what-is-their-purpose – Nigel Ren Aug 09 '18 at 05:27
  • Also you should reconsider what you yourself wrote: "The array gets updated whenever admin adds new location"... An "admin" changing something will not directly modify any values in an executing script. Keep in mind that requests served by php scripts are handled in separate processes or threads. So to me this reads as if you have a major design flaw in the first place when you fetch some array from some resource which you then do not really want to use. – arkascha Aug 09 '18 at 05:39
  • 1
    **I will need to access those variables and assign their values to javascript variable** How will you do this if you don't know which variables exist? Anything you can do with dynamically-created variables can almost always be done more easily using an array. – Barmar Aug 09 '18 at 05:44
  • For god's sake, even I can't remember what is this now! – maverick Oct 21 '22 at 03:39

5 Answers5

0

if you want individual variables then you can use this program.

<?php

$locations = ['name1', 'name2', 'name3', 'name4'];

$alocation = array();
$i = 1;
foreach ($locations as $key => $value) {
  $alocation['location' . $i] = $value;
  $i++;
}
extract($alocation);
echo '<pre>'; print_r($alocation); echo '</pre>';
echo $location1 . '--' . $location2 . '--' . $location3 . '--' . $location4;
?>
Viswanath Polaki
  • 1,357
  • 1
  • 10
  • 19
0

try this

$l = array('name1', 'name2', 'name3', 'name4');
$length = sizeof($l);
for ($i = 0; $i < $length; $i++){
    ${"location".($i+1)} = $l[$i];
}
echo "$location1<br />$location2<br />$location3<br />$location4<br />";

Output

name1 name2 name3 name4

Chirag
  • 363
  • 2
  • 12
0

You can initialize dynamic variables or use extract function.

Snippet for creating dynamic variables.

$locations = [ 'name1', 'name2', 'name3', 'name4' ];
foreach ($locations as $key => $val){
${'location'.($key +1)} = $val;
}
echo $location3;

Output:

name3

Live demo

Snippet for extract with prefix

$locations = [ 'name1', 'name2', 'name3', 'name4' ];
extract ($locations, EXTR_PREFIX_ALL, 'location');
echo $location_1;

Output:

name2

Live demo

Note: prefix start from zero leading with underscore. Extract Docs

Shahnawaz Kadari
  • 1,423
  • 1
  • 12
  • 20
0

I just needed to get an array from php to javascript

Following code does exactly that

<?php $location = [....some values in this array...]; ?>

<script>var name = <?php echo json_encode( $location ) ?>;</script>

Now whenever the array in php script is updated, the array in javascript is automatically updated.

miken32
  • 42,008
  • 16
  • 111
  • 154
maverick
  • 141
  • 1
  • 13
-1

I don't know why you want to do this but I think the best way if you use JSON object so you can transform your array to JSON (json_encode, json_decode) very simplify and after that you can handle any way you want.

szaboolcs
  • 41
  • 1
  • 6
  • 1
    What does JSON have to do with variable variables? JSON is for serializing data so it can be transmitted or saved, it has nothing to do with creating new variables. – Barmar Aug 09 '18 at 05:42
  • "Reason for this is I will need to access those variables and assign their values to javascript variable for displaying their name in map." Are you thinking is it the best way? The variables absolutely unnecessary in this case. – szaboolcs Aug 09 '18 at 06:29
  • I agree, see the comment I posted to the question itself. But if you're going to answer the question, answer it as they asked. – Barmar Aug 09 '18 at 06:30
  • Are you talking about something like `var locations = ;`? You should show this in the answer instead of oblique references to the functions. – Barmar Aug 09 '18 at 06:34
  • I didn't see your comment sry, but I don't agree the second part of your sentence because i think we should show the right way :) a note only :P now it's better if we used the "let" instead of "var" (ES6) – szaboolcs Aug 09 '18 at 06:47