Php get array elememnt with $$
I know it
$var1 = "10";
$var2 = "var1";
it echo $$var2
gives 10
I want with this array
I have an array
$intake_arr = array(5=>10,7=>20,8=>30,9=>40,10=>50,11=>60,12=>70);
I have some logic that will select one array from many arrays, the whole array will look like $ consum_arr
if i do this $target_arr = "intake_arr";
can it $$target_arr[5]
give 10? i tried but i didn't make this value 10, how can i achieve this with an array
source to share
Your statement ( $$target_arr[5]
) is ambiguous. PHP doesn't know what you actually want to say: do you mean: use a $target_arr[5]
value and add $
to use that as a variable, or do you want to use a value $target_arr
and get the fifth element of that array?
Obviously this is the latter, but PHP doesn't know this. To eliminate your expression, you must use curly braces:
${$target_arr}[5];
This will give 10. See the manual on variable variables for details
Note:
As people say in the comments and deleted answers: variable variables like the one you use is risky business. 9/10 this can and should be avoided. This makes your code harder to read, more error-prone, and when combined with these two major flaws is a killer: it makes your code incredibly hard to debug .
If this is just a technical exercise, consider this post as friendly advice. If you got this from some sort of tutorial / blog or other type of online resource: never visit this site again.
If you are actually working on a piece of code and you have decided to solve a specific problem with the vars variables, then perhaps post your code in the code overviewand let me know, I will take a look and try to offer constructive criticism that will help you on your way to a better solution.
Since what you are actually trying to do is copy the array into another variable, this is pretty straightforward. PHP offers many ways to do this:
Copy on assignment:
PHP copies arrays on default assignment, so this means that:
$someArray = range(1,10);//[1,2,3,4,5,6,7,8,9,10]
$foo = $someArray;
Assigns a copy to the $someArray
variable $foo
:
echo $foo[0], ' === ', $someArray[0];//echoes 1 === 1
$foo[0] += 123;
echo $foo[0], ' != ', $someArray[0];//echoes 123 != 1
I can change the value of one of the elements in the array without affecting the original array because it was copied.
There is a risk for this, since you start out with JSON encoded data, chances are you will end up with something like:
$obj = json_decode($string);
echo get_class($obj));//echoes stdClass, you have an object
Objects are passed and assigned by reference by default, which means that:
$obj = new stdClass;
$obj->some_property = 'foobar';
$foo = $obj;
$foo->some_property .= '2';
echo $obj->some_property;//echoes foobar2!
Change the property via $foo
and the object $obj
will change too. Simply because they both refer to the same object.
Slice Array:
A more general way for frontend developers (basically stemming from the JS habbit I think) is to use array_slice
that guarantees a copy of the array is returned. with the added perk that you can specify how many of the items you need in your copy:
$someArray = range(1,100);//"large" array
$foo = array_slice($someArray, 0);//copy from index 0 to the end
$bar = array_slice($someArray, -10);//copy last 10 elements
$chunk = array_slice($someArray, 20, 4);//start at index 20, copy 4 elements
If you don't want to copy the array, but rather extract the slice from the original, you can splice
array (as in split + slice):
$extract = array_splice($someArray, 0, 10);
echo count($someArray);//echoes 90
This removes the first 10 elements from the original array and assigns them $extract
Spend some time browsing the countless (well, about a hundred) array functions PHP has to offer.
source to share
Try the following:
$intake_arr = array(5=>10,7=>20,8=>30,9=>40,10=>50,11=>60,12=>70);
$target_arr = 'intake_arr';
print ${$target_arr}[5]; //it gives 10
For a simple variable, curly braces are optional. But when you use an array element, you must use curly braces; e.g.: ${$target_arr}[5];
... Curly braces are used as a standard when using variable interpolation rather than concatenation. Usually variable interpolation is slow, but concatenation can also be slower if you have too many variables to concatenate. Have a look here at php variable variables http://php.net/manual/en/language.variables.variable.php
source to share