0

I have an array, say

$current_file_data=
['step1'=>
         ['step2'=1]
]

I want to have a simple function, that would take a string, and simply deliver the deeper element of this array, say something like:

function deeper_element($path,$array) {

return $array{$path};

}

so if using

$current_file_data

I would use deeper_element('['step1']['step2']',$current_file_data)

and return 1 from it (see array above)

i simple tried $array{$path} but it was not enough

2
  • 1
    maybe this answer help you..link Commented Dec 3, 2015 at 8:01
  • So you only need the deepest value of your multidimensional array i.e. 1 Commented Dec 3, 2015 at 8:46

4 Answers 4

1

Let's suppose that your $path is a string, which contains keys separated by a separator. For instance, if your separator is /, then the example of

a/b/c/d

would mean $array["a"]["b"]["c"]["d"].

Let's see the function:

function getInnerArray($array, $path, $separator = "/") {
    $keys = explode($separator, $path);
    $temp = $array;
    for ($keys as $key) {
        if (isset($temp[$key])) {
            $temp = $temp[$key];
        } else {
            return null;
        }
    }
    return $temp;
}

Explanation: The separator can be anything you like, but for the sake of simplicity, I have defined a default value. An array of keys will be the result of explode, using the separator. A temp is initialized to the array and a cycle loops through the keys. Upon each step, temp will be refreshed to the inner element found by key if exists. If not, then the path is invalid and null is returned.

Sign up to request clarification or add additional context in comments.

Comments

1

I dont think there is dynamic feature for this. And please check the syntax used for declaring arrays.

<?php
$data=array(
'step1'=>array(
    'step2'=>1
    )
);
function get_deep($path,$data){
$path=explode(',',$path);
return $data[$path[0]][$path[1]];
}
echo get_deep("step1,step2",$data);
?>

This will result 1 as output. And if you want to access file contents,in such case json files you can create array like this

$data=json_decode(filestream,true);

Comments

0
    <?php

        function array_keys_multi(array $array)
            {
                $keys = array();

                foreach ($array as $key => $value) {
                    $keys[] = $key;

                    if (is_array($array[$key])) {
                        $keys = array_merge($keys, array_keys_multi($array[$key]));
                    }
                }

                return $keys;
            }

        $current_file_data = array('step1' => array('step2'=>1));

        $arr_keys = array_keys_multi($current_file_data);

        if( in_array('step2', $arr_keys) )
            echo "FOUND";

    ?>

Comments

0

thanks for the inspiring feedback. But I got it working doing the following:

Class:

    <?php


namespace App\Tools\Arrays;


class DeepArrayExtractor
{

    /**
     * if path to deeper array element exists, return it
     * otherwise return the complete original array
     * @param array $deepArray
     * @param $pathBitsString
     * @param $separator
     * @return array
     */
    public static function deeperArrayElement(array $deepArray, $pathBitsString, $separator)
    {

        $currentArray = $deepArray;
        $pathBits = explode($separator, $pathBitsString);
        foreach ($pathBits as $bit) {
            if (isset($currentArray[$bit])) {
                $deepArrayElement = $currentArray[$bit];
                $currentArray = $deepArrayElement;
            } else {
                return $deepArray;
            }
        }
        return $deepArrayElement;
    }
}

and unit tests

<?php


namespace App\Tools\Arrays;


class DeepArrayExtractorTest extends \TestCase
{


    public function setUp()
    {
        parent::setUp();
    }

    public function tearDown()
    {
        parent::tearDown();
    }


    /**
     * @test
     * @group DeepArrayExtractorTest1
     */
    public function getDeepArrayCorrectly()
    {
        $deepArray = [
            'step1' =>
                ['step2' => 1]
        ];
        $separator = '---';
        $path = 'step1---step2';
        $deepArrayElement = DeepArrayExtractor::deeperArrayElement($deepArray, $path, $separator);
        $this->assertEquals(1, $deepArrayElement);
    }

    /**
     * @test
     * @group DeepArrayExtractorTest2
     */
    public function deepArrayDoesNotExistSoOriginalArrayReturned()
    {
        $deepArray = [
            'step1' =>
                ['step3' => 1]
        ];
        $separator = '---';
        $path = 'step1---step2';
        $deepArrayElement = DeepArrayExtractor::deeperArrayElement($deepArray, $path, $separator);
        $this->assertEquals($deepArray, $deepArrayElement);
    }

}

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.