2

Let's say I have the following multi-dimensional array:

$fruits['apple']['name'] = 'macintosh';

Is there any way of referencing the entire key path in a single variable?

I would like to somehow do the following:

$path = "['apple']['name']";
echo $fruits[$path];
//output would be "macintosh"
asked Jun 6, 2012 at 20:24
6
  • &references comes to my mind.. Commented Jun 6, 2012 at 20:29
  • @vizvi Can you expand a bit on that? Thanks. Commented Jun 6, 2012 at 20:30
  • you could call: $fruits2 = &fruits['apple']; echo $fruits2['name']; would give "macintosh" Commented Jun 6, 2012 at 20:31
  • 1
    maybe interesting to you: stackoverflow.com/questions/8376602/… if you would like $fruits['apple/name'] to be valid Commented Jun 6, 2012 at 20:34
  • You could always use classes instead of arrays. Commented Jun 6, 2012 at 20:35

5 Answers 5

3

Is there any way of referencing the entire key path in a single variable?

1) In a way like this: $fruits[$variable], the answer is no, there isn't.

Of course, there are several ways you can split a single $variable in two or more, and then use its parts separetely ($fruits[$part1][$part2])

This is a generic solution:

function get_path($array, $path)
{
 $value = $array;
 $parts = explode("']['", trim($path, "[']"));
 foreach($parts as $key)
 {
 $value = $value[$key];
 }
 return $value;
}
$fruits['apple']['name'] = 'macintosh';
$path = "['apple']['name']";
echo get_path($fruits, $path);
// output = 'macintosh'

2) As also pointed, you could use "eval", which is not recommended:

$fruits['apple']['name'] = 'macintosh';
$path = "['apple']['name']";
eval('echo $fruits' . $path . ';');
// output = 'macintosh'

3) Finally, if you want to access an element of the array using a reference variable, then simply:

$fruits['apple']['name'] = 'macintosh';
$path &= $fruits['apple']['name'];
echo $path; // output = 'macintosh'
$path = 'MSX';
echo $fruits['apple']['name']; // output = 'MSX'
answered Jun 6, 2012 at 20:28
Sign up to request clarification or add additional context in comments.

Comments

1

There is obviously the horrible, never-to-be-used approach: eval()

But if you really want to do something like this, I would prefer something like this approach:

function &array_value_by_address (&$array, $address, $addressSeparator = '/') {
 $parts = explode($addressSeparator, $address);
 $thisLevel = array_shift($parts);
 if (isset($array[$thisLevel])) {
 if ($parts) {
 $ref = &array_value_by_address($array[$thisLevel], implode($addressSeparator, $parts));
 return $ref;
 } else {
 return $array[$thisLevel];
 }
 }
}

Because of the references in the function declaration, it's also possible to catch the return value in a variable and use it to modify the source array. This is optional - if you omit the & prefix at call time the caught value will simply be a copu and not a reference.

The caveat though - the function will return NULL if the "address" within the array does not exist, but there is no way to distinguish between this situation and a NULL value stored within the array.

See it working

answered Jun 6, 2012 at 20:39

Comments

1

J Brun's #3 answer is your best bet, however, his answer is not correct as he has demonstrated the bitwise-AND operator (&=) and not the referential assignment. This is the corrected code:

$fruits['apple'] = array( 'name' => 'macintosh' );
$path = &$fruits['apple']['name'];
echo $path; // outputs 'macintosh';
answered Jul 8, 2013 at 2:34

Comments

0

References might be what you need:

$path = &$fruits['apple']['name'];

Now every time you would access $path, it's the value of $fruits['apple']['name'] that will be used (or changed, etc.).

answered Jun 6, 2012 at 20:30

1 Comment

Thanks. I think this is the most promising answer so far for my case.
0

What you want to do is not directly possible - it would be ambigous if variables are chained.

You will need to write an accessor function such as

define('SEPARATOR','|');
function getElement($array,$path) {
 $path=explode(SEPARATOR,$path);
 $out=&$array;
 foreach ($path as $step) {
 if (!isset($out[$step])) return null;
 $out=$out[$step];
 }
 return $out;
}
$path="apple|name";
echo getElement($fruits,$path);
answered Jun 6, 2012 at 20:32

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.