1

Im wanting to dynamically access a multidimensional array, as i have a need to display the full path within a config file. Below are a couple of examples of how i know you can access the value at a certain level.

echo $results['Data']['MetaAttrListId']['0'];

$string = "MetaAttrListId";
echo $results['Data'][$string]['0'];

But what i want to be able to do is provide the full location/path to that area in a string. Like so.

$string = "['Data']['MetaAttrListId']['0']";
echo $results[$string];

the output of the multidimensional array im accessing.

    (
        [Data] => Array
            (
                [MetaTitle] => Array
                    (
                        [0] => Vauxhall combo 1.3 cdti in stunning condition low mileage long mot till august
                    )

                [MetaAttrListId] => Array
                    (
                        [0] => Posted
                        [1] => Make
                        [2] => Model
                        [3] => Year
                        [4] => Mileage
                        [5] => Seller type
                        [6] => Body type
                        [7] => Fuel type
                        [8] => Transmission
                        [9] => Colour
                        [10] => Engine size
                    )

                [MetaAttrListValue] => Array
                    (
                        [0] => 1 day ago
                        [1] => Vauxhall
                        [2] => COMBO
                        [3] => 2005
                        [4] => 79000
                        [5] => Private
                        [6] => Car Derived Van
                        [7] => Diesel
                        [8] => Manual
                        [9] => Red
                        [10] => 1248
                    )

            )

        [Error] => 
    )
1

1 Answer 1

1

You might try this function I built the other day (also inspired by another stackoverflow thread I don't find right now, but there are similar questions)

Use it like value_in($arrayThingy, 'path.to.that.entry') or value_in($arrayThingy, 'path/to/that/entry', '/')

Hope it helps, and please report any failure if you find one : )

/**
 * value_in
 * 
 * @param  mixed    $haystack   array or object or nested mix of both
 * @param  string   $path       path in any token-separated notation
 * @param  string   $token      path separator token
 * @return mixed                resolved value
 */
function value_in($haystack, $path, $token = ".") {
    $path = trim($path, $token); // path.to.place
    $segments = explode($token, $path); // ["path", "to", "place"]
    $remains = $haystack;
    foreach ($segments as $segment) {

        if (gettype($remains) === 'array' && isset($remains[$segment])) {
            $remains = $remains[$segment];
        } else if (gettype($remains) === 'object' && isset($remains->$segment)) {
            $remains = $remains->$segment;

        } else {
            return null;
        }
    }
    return $remains;
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.