简体   繁体   中英

PHP: Multidimensional array foreach echo form passing third value

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" />

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 ???

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';
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM