0

Possible Duplicate:
Dynamic array keys

I have an array $Values which is set up like this

$Values
  1
  2
  3
  a
    b
      c
  4

which is nested. and I have a key like this: $key = "a"]["b"]["c"; Can I now do: Values[$key], ti get the value in c ?

@Edit Simply said: I want to get the value from array $Values["a"]["b"]["c"] By doing $Values[$key]. What should my key be then?

Community
  • 1
  • 1
Nealv
  • 6,856
  • 8
  • 58
  • 89
  • 6
    Have you tried it at all? Or are you one of these people who prefers to learn by being spoon-fed rather than by experimenting. – Mark Baker Feb 12 '12 at 17:42
  • It's difficult to understand what exactly you are trying to accomplish. [Read the manual on arrays.](http://php.net/manual/en/language.types.array.php) – Josh Feb 12 '12 at 17:43
  • Yes I have tried it, and no, I am not the kind of spoon fed person. – Nealv Feb 12 '12 at 18:35
  • 1
    You cant do what you want to do. – Toby Allen Feb 12 '12 at 19:19
  • Thx Toby, that was actually the best comment :). Exactly what I needed to know (no sarcasm). Tnx Mark for the solution – Nealv Feb 12 '12 at 20:27

3 Answers3

2

No, you only can get individual keys from variables. Depending on what you really want to do you could use references to your array elements.

raphinesse
  • 19,068
  • 6
  • 39
  • 48
2

No, but you can extract the result:

$Values = array( '1' => 'ONE',
                 '2' => 'TWO',
                 '3' => 'THREE',
                 'a' => array( 'b' => array( 'c' => 'alphabet') ),
                 '4' => 'FOUR'
               );

$key = '"a"]["b"]["c"';

$nestedKey = explode('][',$key);
$searchArray = $Values;
foreach($nestedKey as $nestedKeyValue) {
    $searchArray = $searchArray[trim($nestedKeyValue,'"')];
}

var_dump($searchArray);

Will only work if $key is valid.

Now how do you get in a situation with a key like this anyway? Perhaps if you explained the real problem, we could give you a real answer rather than a hack.

Mark Baker
  • 209,507
  • 32
  • 346
  • 385
2

Nah you can't. This is invalid syntax.

Hover you can do:

$key = 'a,b,c';
// or:
$key = serialize( array( 'a','b', 'c'));
// or many other things

And than implement your array-like class which will implement ArrayAccess or ArrayObject (let's way you'll stick with $key = 'a,b,c';):

class MyArray extends ArrayAccess {

protected $data = array();

protected &_qetViaKey( $key, &$exists, $createOnNonExisting = false){
  // Parse keys
  $keys = array();
  if( strpos( $key, ',') === false){
    $keys[] = $key;
  } else {
    $keys = explode( ',', $key);
  }

  // Prepare variables
  static $null = null;
  $null = null;
  $exists = true;

  // Browse them
  $progress = &$this->data;
  foreach( $keys as $key){
      if( is_array( $progress)){
         if( isset( $progress[ $key])){
            $progress = $progress[ $key];
         } else {
            if( $createOnNonExisting){
               $progress[ $key] = array();
               $progress = $progress[ $key];
            } else {
               $exists = false;
               break;
            }
         }
      } else {
          throw new Exception( '$item[a,b] was already set to scalar');
      }
  }

  if( $exists){
      return $progress;
  }
  return $null;
}

public offsetExists( $key){
    $exists = false;
    $this->_getViaKey( $key, $exists, false);
    return $exists;
}

// See that we aren't using reference anymore in return
public offsetGet( $key){
    $exists = false;
    $value = $this->_getViaKey( $key, $exists, false);
    if( !$exists){
       trigger_error( ... NOTICE ...);
    }
    return $value;
}

public offsetSet ( $key, $val){
    $exists = false;
    $value = $this->_getViaKey( $key, $exists, true);
    $value = $val;
}

}

// And use it as:
$array = MyArray();
$array['a']['b']['c'] = 3;
$array['a,b,c'] = 3;

Or implement function:

public function &getArrayAtKey( $array, $key){
    // Similar to  _qetViaKey
    // Implement your own non existing key handling
}
Vyktor
  • 20,559
  • 6
  • 64
  • 96