Heres what I want to do:
<?php
$array = array("array1" => array(), "array2" => array());
function mdparser(){
for($i = 0;$i < func_num_args();$i++){
//foreach arguement go 1 child deeper
return $array["array1"] //if second argument exists ["array2"];
?>
I need this to parse multidimensional arrays independantly , so I can use it on any Multi dimensional array.
Still just guessing at what you're trying to achieve
$this->myArray = array("key1" => array("key2" => array(1,2,3)));
protected function mdparser(){
$result = $this->myArray;
for($i = 0;$i < func_num_args();$i++){
$result = $result[func_get_arg($i)];
}
return $result;
}
$result = this->mdparser('key1', 'key2', 1);
PHP >= 5.6 allows variadics to simplify this:
protected function mdparser(...$args){
$result = $this->myArray;
foreach($args as $arg){
$result = $result[$arg];
}
return $result;
}
Just pass the array to the method when you call it, assuming you are calling it from somewhere where $array
is in scope
Also change the array you are building so you have a known, easily processible index i.e. 0.1.2... and not 'array1', 'array2',....
<?php
$array = array( array(), array() );
function mdparser($array){
return $array[count($array)-1];
}
$last_array = mdparser($array);
?>
This should return the last array within the outer array.
Doesnt matter now ive found a way thanks anyways:
function search_array(){
$args = func_get_args();
$arg_num = func_num_args();
for($i = 0;$i < $arg_num;$i++){
$arg = $args[$i];
//foreach arguement go 1 child deeper
$array = $this->array[$arg];
$this->array = $array;
//if second argument exists ["array2"];
}
}