Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Hi Im passing from form values in multidimensional array. Values that I am passing look like this. Third value is passed from html form.

<input type=\"text\" name=\"input[".$row[Id]."][".$record[Name]."][]\" size=\"2\" />

input[1][1][]
input[1][2][]
input[1][3][]
input[2][1][]
input[2][2][]
input[2][3][]

then I try to foreach them but I am stuck.

foreach($_POST[input] as $name => $value){
    foreach($value as $inner_value =>$value){
        foreach($value as $inner_inner_value => $value){
            echo "Menu: {$name} submenu: {$inner_value} subsubmenu :{$inner_inner_value}\n<br><br>";
        }
    }
}

it is echoing this:

Menu:1 submenu: 1 subsubmenu: 0
Menu:1 submenu: 2 subsubmenu: 0
Menu:1 submenu: 3 subsubmenu: 0
Menu:2 submenu: 1 subsubmenu: 0
Menu:2 submenu: 2 subsubmenu: 0
Menu:2 submenu: 3 subsubmenu: 0

But i need to achieve this

Menu:1 submenu: 1 subsubmenu: value entered into form field.
Menu:1 submenu: 2 subsubmenu: value entered into form field.
Menu:1 submenu: 3 subsubmenu: value entered into form field.
Menu:2 submenu: 1 subsubmenu: value entered into form field.
Menu:2 submenu: 2 subsubmenu: value entered into form field.
Menu:2 submenu: 3 subsubmenu: value entered into form field.

third value is posted from HTML basically i dont know how can I pass third value to array using form.

<input type="text" name="input[THIS IS OK][THIS IS OK][value ENTERED in FORM FIELD???]" size="2" />
share|improve this question

2 Answers 2

up vote 0 down vote accepted

I don't know if i get what your problem is but if it's what i think this will do it... if you post the input[THIS IS OK][THIS IS OK] and the third dimenstion is the value that this input type posts then you should change the name/id of the input element just like

input[THIS IS OK][THIS IS OK] and when you post you can get the value $val= $_POST[input[".$i."][".$j."]"]; and this $val will hold the value from the post or the 3rd dimension you want ....and you have it $i-first dimension $j-second dimension and $val-third dimension ??

was that what u were looking for ???

share|improve this answer
1  
thanks so simple :) –  user1508136 Feb 6 '13 at 16:35

try it:

protected $printString = '';
function magicRecursiveArrayRunner($someArray){
    if(is_array($someArray)){
        foreach($someArray as $key=>$value){
            if(is_array($value)){
                $this->printString .= "Menu ".$key;
                self::magicRecusiveArrayRunner($value);
            }else{
                $this->printString .= " submenu ".$key;
            }
        }
    }else{
         $this->printString .= " subsubmenu ".$key . '\br';
    }
}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.