0

i know this

$var1 = "10";
$var2 = "var1";

then echo $$var2 gives us 10

i want to this with array

i have array

 $intake_arr = array(5=>10,7=>20,8=>30,9=>40,10=>50,11=>60,12=>70);

i have some logic that will pick one array from set of array , all array will look like $intake_arr

if i do this $target_arr = "intake_arr";

then can $$target_arr[5] will yield 10? i tried but i didnt that 10 value, how can i achieve this with array

3
  • 2
    Whatever it is you're trying to do, this is almost certainly the wrong way to do it.. Commented Nov 26, 2013 at 11:45
  • @mark i know this is wrong , i am getting errors, so can you suggest better way to that ? Commented Nov 26, 2013 at 11:47
  • I believe he means your whole concept of doing stuff. Picking arrays from arrays to form arrays oO looks like a good case for a storage object or perhaps even a list or maybe..... a database oO Commented Nov 26, 2013 at 11:49

3 Answers 3

5

Your statement ($$target_arr[5]) is ambiguous. PHP doesn't know what you actually want to say: Do you mean: use $target_arr[5]'s value and prepend the $, to use that as a variable, or do you want to use the value of $target_arr, and get the fifth element of that array?
Obviously it's the latter, but PHP doesn't know that. In order to disambiguate your statement, you have to use curly braces:

${$target_arr}[5];

That'll yield 10. See the manual on variable variables for details

Note:
As people said in comments, and deleted answers: variable variables, like the one you're using is risky business. 9/10 it can, and indeed should be avoided. It makes your code harder to read, more error prone and, in combination with the those two major disadvantages, this is the killer: it makes your code incredibly hard to debug.
If this is just a technical exercise, consider this note a piece of friendly advice. If you've gotten this from some sort of tutorial/blog or other type of online resource: never visit that site again.
If you're actually working on a piece of code, and you've decided to tackle a specific problem using variable vars, then perhaps post your code on code-review, and let me know, I'll have a look and try to offer some constructive criticism to help you on your way, towards a better solution.


Since what you're actually trying to do is copying an array into another variable, then that's quite easy. PHP offers a variety of ways to do that:

Copy by assignment:
PHP copies arrays on assignment, by default, so that means that:

$someArray = range(1,10);//[1,2,3,4,5,6,7,8,9,10]
$foo = $someArray;

Assigns a copy of $someArray to the 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 array's elements without that affecting the original array, because it was copied.
There is a risk to this, as you start working with JSON encoded data, chances are that you'll end up with something like:

$obj = json_decode($string);
echo get_class($obj));//echoes stdClass, you have an object

Objects are, by default, passed and assigned by reference, which means that:

$obj = new stdClass;
$obj->some_property = 'foobar';
$foo = $obj;
$foo->some_property .= '2';
echo $obj->some_property;//echoes foobar2!

Change a property through $foo, and the $obj object will change, too. Simply because they both reference exactly the same object.

Slice the array:
A more common way for front-end developers (mainly, I think, stemming from a JS habbit) is to use array_slice, which guarantees to return a copy of the array. with the added perk that you can specify how many of the elements you'll be needing 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 a section out of the original you can splice the 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 to $extract

Spend some time browsing the countless (well, about a hundred) array functions PHP offers.

Sign up to request clarification or add additional context in comments.

4 Comments

thanks for answer with explanation and hope you will help in future also
@ddw147: Seeing as I'm somewhat addicted to SO, I might well help you out in the future... And like I said in my last edit: if you're looking for some tips/criticism of your code, I'm an active "contributing" member of the codereview site, too. I mainly focus on PHP there, lately, I'm more active in the C-tagged questions for this site :)
i read your note, if its unsafe then can i copy one array to another, effectively than this. can you tell which is better variable variables or copying array
@ddw147: added some more explanation on the copying of arrays, splicing, reference pitfalls in some cases and what have you... hope some of it makes sense & is useful :)
5

${$target_arr}[5]

PHP: Variable variables

1 Comment

+1 for beating me to the same answer... I was busy typing up my, slightly more verbose, answer :)
2

Try this one:

$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, braces are optional.But when you will use a array element, you must use braces; e.g.: ${$target_arr}[5];.As a standard, braces are used if variable interpolation is used, instead of concatenation.Generally variable interpolation is slow, but concatenation may also be slower if you have too many variables to concatenate.Take a look here for php variable variables http://php.net/manual/en/language.variables.variable.php

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.