从数组中获取的值不会出现

时间:2017-08-03 09:51:53

标签: php

我使用Javascript及其变量在PHP中生成嵌套数组,我使用隐藏输入发送。我想捕获生成的数组并在页面上以适当的形式显示它,我写了下面的脚本,但它不起作用。我做错了什么?

PHP数组

[section] => Array
        (
            [0] => Array
                (
                    ['sectionwidth'] => 525
                    ['sectionheight'] => 500
                    ['bg'] => eaeaea
                    ['sectioncolor'] => c0c0c0
                    ['input'] => Array
                        (
                            [0] => Array
                                (
                                    ['inputtext'] => pyk pyk
                                    ['inputwidth'] => 500
                                )

                            [2] => Array
                                (
                                    ['inputtext'] => cyk cyk
                                    ['inputwidth'] => 500
                                )

                        )

                )

            [1] => Array
                (
                    ['sectionwidth'] => 525
                    ['sectionheight'] => 500
                    ['bg'] => 222222
                    ['sectioncolor'] => ffffff
                    ['input'] => Array
                        (
                            [1] => Array
                                (
                                    ['inputtext'] => pam pam
                                    ['inputwidth'] => 500
                                )

                            [3] => Array
                                (
                                    ['inputtext'] => no no
                                    ['inputwidth'] => 500
                                )

                        )

                )

        )

PHP代码

foreach ($_POST['section'] as $sekcja) {
            echo '<div style="width: '.$sekcja['sectionwidth'].'px; height: '.$sekcja['sectionheight'].'px; background: #'.$sekcja['bg'].'; color: #'.$sekcja['sectioncolor'].';">';
            foreach ($sekcja['input'] as $input) {
                '<p style="width: '.$input['inputwidth'].'px;">'.$input['inputtext'].'</p>';
            }
            echo '</div>';
        }

2 个答案:

答案 0 :(得分:2)

foreach ($_POST['section'] as $sekcja) {
            echo '<div style="width: '.$sekcja['sectionwidth'].'px; height: '.$sekcja['sectionheight'].'px; background: #'.$sekcja['bg'].'; color: #'.$sekcja['sectioncolor'].';">';
            foreach ($sekcja['input'] as $input) {
                echo '<p style="width: '.$input['inputwidth'].'px;">'.$input['inputtext'].'</p>';
            }
            echo '</div>';
        }

你错过了foreach中的回声

答案 1 :(得分:1)

foreach ($_POST['section'] as $sekcjda) {
      foreach($sekcjda as $sekcja){
            echo '<div style="width: '.$sekcja['sectionwidth'].'px; height: '.$sekcja['sectionheight'].'px; background: #'.$sekcja['bg'].'; color: #'.$sekcja['sectioncolor'].';">';
            foreach ($sekcja['input'] as $input) {
              echo '<p style="width: '.$input['inputwidth'].'px;">'.$input['inputtext'].'</p>';
            }
            echo '</div>';
        }
}

你在foreach中缺少一个回声,并且还返回了数组的返回数组,所以你需要另一个循环来获得sectionheight和所有其他东西