Say for example that I have this variable
$p0001 = array("title"=>"This is the title","name"=>"Just Me");
And the URL is
https://www.example.com?id=p0001
How do I get the correct array from PHP? I've tried
echo $_GET["id"]["title"];
To explain little more, say I have two array variables. I want it to echo the "title" from from the array $p0001. So how do I make sure I gets the variable I put in $_GET?
You can use Variable variables
So, your variable name is $p0001. Your GET parameter id is basically a pointer to the variable name, so using variable variables, we can reference the variable we're looking for:
$varname = $_GET['id']; // $varname = 'p0001';
$$varname; // this is basically $p0001
$$varname['title']; // and you can get your title from $p0001
You could also check if a variable exists before using with isset($$varname)
Related
Is it possible to check if a variable exist using a variable value?
Like
//some variables:
$variable_a;
$variable_b;
$variable_c;
$variable_d;
$variable = '$variable_b';
if (the content $variable which is '$variable_b' exists == true){
}
or how to make a variable value into a variable?
Like
$variable = 'variable_name';
...some code to make 'variable_name' a variable
You can use variable variables in PHP, and then check if such a variable has a value. For instance:
$variableA = 'Hello';
$variableB = 'variableA';
echo ${$variableB};
returns Hello on your screen. You can also check if $variableA has a value by doing:
if (isset(${$variableB})) {
....
}
Note that in your question you have variables that have no value, they are not set. The whole purpose of variables is to have a value, hence their name, so your variables are some kind of zombies, not really alive, not really dead. They are not set, so isset() will return false.
I'm trying to work out how to get values from one of three arrays based on the array name.
$ABC001 = array('A'=>'10','B'=>'2','C'=>'1.0');
$ABC002 = array('A'=>'20','B'=>'4','C'=>'1.1');
$ABC003 = array('A'=>'30','B'=>'6','C'=>'1.2');
I have a variable passed to my script it will be contain something like ABC#001 or ABC#002
I'm removing the # so the var value now matches the array name/
$test = str_replace('#','',$var);
If I do var_dump ( $$test ) I get all the values from the correct array, but if I do echo $$test['A'] or echo $$test[0] I don't get the value from the first key in the correct array.
Can someone advise how to do this.
Thanks
try this ${$test} to get the values of the array
<?php
$ABC001 = array('A'=>'10','B'=>'2','C'=>'1.0');
$ABC002 = array('A'=>'20','B'=>'4','C'=>'1.1');
$ABC003 = array('A'=>'30','B'=>'6','C'=>'1.2');
$var = "ABC#002";
$test = str_replace('#','',$var);
var_dump(${$test}['A']);
In order to use variable variables with arrays, you have to resolve an ambiguity problem. That is, if you write $$test['A'] then the parser needs to know if you meant to use $test['A'] as a variable, The syntax for resolving this ambiguity is: ${$test}['A'] . Please check the documention here PHP Variable Variable
I have a array containing to which I have created local variables from using the extract() function with the EXTR_PREFIX_ALL flag set. Afterwards, I called compact() on the new prefixed variables created by extract() but displaying the array created from compact() using print_r() gives an empty array(). Sample code follows:
<?php
$cities = array('City1' => "Chicago", 'City2' => "Boston");
extract($cities, EXTR_PREFIX_ALL, "new");
echo "City 1: {$new_City1} City 2: {$new_City2}" . "<br><br>";
$new_cities = compact($new_City1, $new_City2);
print_r($new_cities);
?>
I am using PHP version 5.6. What am I doing wrong here?
In your current code, you're actually providing the values of each variables you're trying to compact, not the name of the variables. It acts like a variable variables behavior. In order to make it work properly, you provide the variable names as strings or in array form:
$new_cities = compact(array('new_City1', 'new_City2'));
// or
$new_cities = compact('new_City1', 'new_City2');
Here's the excerpt from the manual:
Parameters
varname1
compact() takes a variable number of parameters. Each parameter can be either a string containing the name of the variable, or an array of variable names. The array can contain other arrays of variable names inside it; compact() handles it recursively.
Give it a try
$new_cities = compact('new_City1', 'new_City2');
print_r($new_cities);
For more info
I want to append some $variables automaticly and set their names numeric
I have a script look like this:
<?php
$i=0;
while($i<=100){
$variable_[$i]=$i;
$i++;
#with "[$i]" I mean their name will be $variable_1 , $variable_2, $variable_3 ...
#they will be automatic increased variables non manual!
}
?>
This is called variable variables.
You can set a variable variable by defining its name inside a variable, such as:
$name = 'variable_' . $i;
and then assign a value to it by doing:
$$name = $i;
Note that variable variables can easily be misused. Make sure you completely understand the repercussions of this feature on your code and the risk of having bugs, and ensure this is the only solution you have, i.e. you can't use an array ($variables[$i] = $i;) instead.
Its better to use Array with key=> value pair. You can build this array dynamically and then loop through it by using foreach.
Is it possible to have a variable in a session name?
Like if I have a variable: $id
I want something like this:
$_SESSION['number'$id]
So that if $id=1
$_SESSION['number1']
And if $id=65
$_SESSION['number65']
Yes - you can do this.
All you have to do is concatenate a string value as the index in the $_SESSION array.
$id= 42;
$result = $_SESSION['number'.$id];
Now $result will be equal to the value of $_SESSION at the index "number42";
The same can be done for any associative array as well.
$any_assoc_array = array('index42'=>'Hooray!');
$id= 42;
$result = $any_assoc_array['index'.$id];
echo $result;
OUTPUT :
Hooray!
In PHP you can place variable names within a string literal if in double-quotes, so the following is perfectly legal. Note that the variable can appear anywhere in the string literal.
$result = $_SESSION["number$id"];