-6

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.

9
  • 3
    Read about variable scope Commented Mar 16, 2015 at 13:04
  • that doesnt help unfortunately Commented Mar 16, 2015 at 13:05
  • 1
    possible duplicate of Parsing PHP Multidimensional Array Commented Mar 16, 2015 at 13:06
  • 1
    The reference to "variable scope" should help eliminate at least one problem: $array doesn't exist in your mdparser() function because it is "out of scope"... you also have other issues, such as missing } Commented Mar 16, 2015 at 13:07
  • 3
    Then show "real" code, not something cobbled together that's syntactically invalid, and with a myriad of other problems Commented Mar 16, 2015 at 13:09

3 Answers 3

0

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;
}
Sign up to request clarification or add additional context in comments.

Comments

0

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.

6 Comments

1. you haven't included $array in the function arguments. 2. read the comments above and you'll notice it's actually part of a class, though that's really the OPs fault.
@Styphon Thanks, woops. Re class, I cannot guess what his class looks like so (s)he will have to extrapolate the answer to the actual situation. (s)he seems enphatic that they dont want to give a real piece of code.
Thanks for trying but it aint working.
Well like they said to your question, give us half a chance by showing some more realistic code.
I cannot its a full class with other 3000 Lines of code...
|
0

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"]; 
}
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.