2

I'm trying to figure out how to take a string, parse it, and get the corresponding array values that match the parsed keys back. I don't know how to properly explain this, so here's an example.

$testArray = array(
    'router' => array(
        'format' => 'xml',
    ),
);

The string I want to parse will look like this - router.format, and I'm obviously expecting back xml as a string. The passed in string can have any arbitrary amount of separating periods. I was thinking of doing recursion somehow, but I can't think of any way in which this will work.

This is very similar to SF2's service container and the way in which you get services back.

Any help would be great!

iLikeBreakfast
  • 1,545
  • 23
  • 46
  • You could split them using `explode()`? This will return an array based on a split delimiter – iswinky Aug 04 '14 at 20:09
  • Yeah I know I can `explode()`, but that will simply return an array containing `router` and `format` - what then? I want to use those values to get back the `format` key from the `router` array from within the `$testArray`... – iLikeBreakfast Aug 04 '14 at 20:11
  • How do you want to handle the cases where the string has more or fewer parts than there are levels in the array? – Patrick Q Aug 04 '14 at 20:11
  • @PatrickQ, I'll just throw an exception. – iLikeBreakfast Aug 04 '14 at 20:11
  • I am a bit confused by what you are asking. It might help if you post some code showing what you might have tried so far. – Mr. Concolato Aug 04 '14 at 20:19

4 Answers4

3

Might need some error checking:

$testArray = array(
    'router' => array(
        'format' => 'xml',
    ),
);
$path = 'router.format';

$result = $testArray;
foreach(explode('.', $path) as $key) {
    if(isset($result[$key])) {
        $result = $result[$key];
    }
}
print_r($result);
AbraCadaver
  • 78,200
  • 7
  • 66
  • 87
1
<?php
    $string = 'router.format';
    $array = array(
        'router' => array(
            'format' => 'xml',
        ),
    );

    $string = explode('.', $string);
    $result = $array;

    for($i = 0; $i < count($string); ++$i)
    {  
        $key = $string[$i];
        if(isset($result[$key]))
            $result = $result[$key];
        else
            break;
    }

    print_r($result);
fiction
  • 566
  • 1
  • 6
  • 21
1

Laravel has a helper function which does this: http://laravel.com/api/source-function-array_get.html#226-251

The code for the function is:

function array_get($array, $key, $default = null)
{
    if (is_null($key)) return $array; 
    if (isset($array[$key])) return $array[$key];
    foreach (explode('.', $key) as $segment)
    {
         if ( ! is_array($array) or ! array_key_exists($segment, $array))
         {
             return value($default);
         }

         $array = $array[$segment];
    }

    return $array;
}

function value($value)
{
     return $value instanceof Closure ? $value() : $value;
}

Usage:

array_get($testArray, 'router.format');
dave
  • 62,300
  • 5
  • 72
  • 93
0

Recursive method.

function array_path($keys, $array) {
    $key = array_shift($keys);
    if (!isset($array[$key])) { return false; }
    return !empty($keys) ? array_path($keys, $array[$key]) : $array[$key];
}

$testArray = array(
    'router' => array(
        'format' => 'xml',
    ),
);
$keys = explode('.', 'router.format');

var_dump(array_path($keys, $testArray));
shudder
  • 2,076
  • 2
  • 20
  • 21