0

I want to send a key of multi dimension array to a function and get the value of that key, it should be an item or and sub array. Imagine I have this function:

public function returnArray($index){
        $arr = [
            'name' => 'ali',
            'children' => [
                '1' => 'reza',
                '2' => 'hasan',
                '3' => 'farhad',
                'info' => [
                    'a',
                    'b',
                    'c'
                ]
            ]
        ];
        return $arr[$index];
    }

and when I call it like this:

returnArray('[name][children][info]')

the result should be info from that array.

What should I do?

Thanks in advance.

George G
  • 7,443
  • 12
  • 45
  • 59

3 Answers3

0

If you are looking to return an array of 1 dimension from an array with 3 dimensions, you could send 3 arguments, $key1, $key2, and $key3, and the return value would be array[$key1][$key2][$key3]

jeff
  • 1
  • 1
0

Just FYI, this code smells bad - re-implementing an array within a string, it makes me think it's probably just a good idea to access the array directly like this:

$arr["name"]["children"]["info"]

But, for the sake of complete answers, let's write a function to do what you want.

Firstly, rather than passing in the index within a single string, functions have parameters already, so let's exploit this feature. Within a function, you can get an array containing all the passed-in parameters using [func_get_args](http://php.net/manual/en/function.func-get-args.php).

// remove the parameter $index, as we don't know how many parameters there will be.
function returnArray(){
    $arr = [
        'name' => 'ali',
        'children' => [
            '1' => 'reza',
            '2' => 'hasan',
            '3' => 'farhad',
            'info' => [
                'a',
                'b',
                'c'
            ]
        ]
     ];

// store reference to the position in the array we care about:
    $position = $arr;

    foreach(func_get_args() as $arg) {

// update the reference to the position according to the passed in parameters.
        $position = $position[$arg];
    }

    return $position;
}

Then we can call the function like this:

returnArray("children", "info");

/* Returns:

array(3) {
  [0]=>
  string(1) "a"
  [1]=>
  string(1) "b"
  [2]=>
  string(1) "c"
}
*/
Greg
  • 21,235
  • 17
  • 84
  • 107
0

You can do it like this:

public function returnArray(){
    $indexes = func_get_args();
    $arr = [
        'name' => 'ali',
        'children' => [
            '1' => 'reza',
            '2' => 'hasan',
            '3' => 'farhad',
            'info' => [
                'a',
                'b',
                'c'
            ]
        ]
    ];
    $tmp  = &$arr;
    while($index = array_shift($indexes)){
          $tmp = &$tmp[$index];
    }
    return $tmp;
}

Then:

 returnArray('name','children','info');

But if you want the result should be info then do:

returnArray('children','info');

Just a way to do this;)

JustOnUnderMillions
  • 3,741
  • 9
  • 12