I have an array of keys for a multidimensional array, and I want to get the value of it (if possible, first checking if it even exists). For example, I have the following array:

$keys = array( 'markets', 0, 'orders', 0, 'parameters', 'planType' );

And from this, I need to programmatically get the value of this:

$request['markets'][0]['orders'][0]['parameters']['planType']

I feel like I'm overcomplicating this...The only method I could think of was with the use of eval, but this seems very potentially insecure:

eval("\$value = \$request['" . implode("']['", $keys) . "'];")
echo $value;

    try

    <?php
    /**
     * Walks through an arrays offsets, allows extendable params/indexes like sprintf
     *
     * @param string $array : The array to go through
     * @param string $index : An index of the array to search through.
     * @return variant      : value if found, boolean false if not.
     */
    function GetArrayIndex( $array , $index , $index ='' )
    {
        $args = func_get_args();
        $value = $array;
    
    unset( $args[0] );
    foreach( $args AS $arg )
    {
        if( !isset( $value[ $arg ] )  )
        {
            return false;
        }
    
        $value = $value[ $arg ];
    }
    
    return $value;
    }
    
    
    $test_array[0][1][3] = "monkey";
    
    
    print_r( GetArrayIndex($test_array , 0 ) );
    print_r( GetArrayIndex($test_array , 0 , 1 ) );
    print_r( GetArrayIndex($test_array , 0 , 1 , 3) );
    
    
    ?>
    
    

      thanks! i knew i was over complicating this, should have waited for the daily caffeine to kick in...i condensed it down a bit for my own purposes:

      // assumes $params is an array (which is a safe assumption in my case)
      function array_descend($struct, $params) {
      
      foreach ($params as $key) {
      	$struct = isset($struct[$key]) ? $struct[$key] : NULL;
      }
      
      return $struct;
      }
        Write a Reply...