Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access array using dynamic path

I have an issue in accessing the array in php.

$path  = "['a']['b']['c']";
$value = $array.$path;

In the above piece of code I have an multidimensional array named $array.

$path is a dynamic value which I would get from database.

Now I want to retrieve the value from $array using $path but I am not able to.

$value = $array.$path

returns me

Array['a']['b']['c']

rather than the value.

I hope I have explained my question properly.

like image 339
Abhishek Sanghvi Avatar asked Sep 26 '13 12:09

Abhishek Sanghvi


2 Answers

You have two options. First (evil) if to use eval() function - i.e. interpret your string as code.

Second is to parse your path. That will be:

//$path = "['a']['b']['c']";
preg_match_all("/\['(.*?)'\]/", $path, $rgMatches);
$rgResult = $array;
foreach($rgMatches[1] as $sPath)
{
   $rgResult=$rgResult[$sPath];
}
like image 94
Alma Do Avatar answered Sep 24 '22 09:09

Alma Do


The Kohana framework "Arr" class (API) has a method (Arr::path) that does something similar to what you are requesting. It simply takes an array and a path (with a . as delimiter) and returns the value if found. You could modify this method to suit your needs.

public static function path($array, $path, $default = NULL, $delimiter = NULL)
{
    if ( ! Arr::is_array($array))
    {
        // This is not an array!
        return $default;
    }

    if (is_array($path))
    {
        // The path has already been separated into keys
        $keys = $path;
    }
    else
    {
        if (array_key_exists($path, $array))
        {
            // No need to do extra processing
            return $array[$path];
        }

        if ($delimiter === NULL)
        {
            // Use the default delimiter
            $delimiter = Arr::$delimiter;
        }

        // Remove starting delimiters and spaces
        $path = ltrim($path, "{$delimiter} ");

        // Remove ending delimiters, spaces, and wildcards
        $path = rtrim($path, "{$delimiter} *");

        // Split the keys by delimiter
        $keys = explode($delimiter, $path);
    }

    do
    {
        $key = array_shift($keys);

        if (ctype_digit($key))
        {
            // Make the key an integer
            $key = (int) $key;
        }

        if (isset($array[$key]))
        {
            if ($keys)
            {
                if (Arr::is_array($array[$key]))
                {
                    // Dig down into the next part of the path
                    $array = $array[$key];
                }
                else
                {
                    // Unable to dig deeper
                    break;
                }
            }
            else
            {
                // Found the path requested
                return $array[$key];
            }
        }
        elseif ($key === '*')
        {
            // Handle wildcards

            $values = array();
            foreach ($array as $arr)
            {
                if ($value = Arr::path($arr, implode('.', $keys)))
                {
                    $values[] = $value;
                }
            }

            if ($values)
            {
                // Found the values requested
                return $values;
            }
            else
            {
                // Unable to dig deeper
                break;
            }
        }
        else
        {
            // Unable to dig deeper
            break;
        }
    }
    while ($keys);

    // Unable to find the value requested
    return $default;
}
like image 41
Alasjo Avatar answered Sep 20 '22 09:09

Alasjo