php:嵌套关联数组不输出corectly

时间:2016-07-12 06:58:20

标签: php json associative-array

我已经使用此代码来测试json解码,但出于某种原因$config['pages'][$i]['inputs'][$j]['type']总是radio。如果我最后做$config['pages'][$i]['inputs']就没问题了,但是一旦我添加输入数字,type总是变为radio
我的代码:

<?php
$configFilePath = $_SERVER["DOCUMENT_ROOT"] . "/wms/config/author_submit.json";
$configFile = fopen($configFilePath, "r") or die("Unable to open file config.json"); // open config file
$config = fread($configFile,filesize($configFilePath)) or die("unable to read config.json"); // read config file
$config = json_decode($config, true) or die('json decoding failed');                    // decode config file
?>

<!DOCTYPE html>
<html>
<head>
</head>
<body>
    <form action="upload.php" method="post" enctype="multipart/form-data"> <!-- start form -->
        <?php
        var_dump($config);
        echo "<br><br>";
        for ($i=0; $i < count($config['pages']); $i++)
        {
            echo "page" . $i . "<br><br>";
            for ($j=0; $j < count($config['pages'][$i]['inputs']); $j++)
            {
                echo $config['pages'][$i]['inputs'][$j]["name"] . "<br>";
                if ($config['pages'][$i]['inputs'][$j]['type'] = "radio")
                {
                    echo $i . $j . "<br>";
                    var_dump($config['pages'][$i]['inputs'][$j])
                    echo "<br><br>";
                }
            }
        }
        ?>
    </form>
</body>
</html>

和author_submit.json

{
    "pages":
    [
        {
            "name": "Page1",
            "inputs":
            [
                {
                    "title": "Catagory",
                    "name": "catagory",
                    "type": "radio",
                    "options":
                    [
                        {
                            "name": "Paper",
                            "value": "paper"
                        },
                        {
                            "name": "Letter",
                            "value": "letter"
                        }
                    ]
                },
                {
                    "title": "Title",
                    "name": "title",
                    "type": "text"
                },
                {
                    "title": "File",
                    "name": "file",
                    "type": "file",
                    "fileName": "?pages[0].inputs[0]"
                },
                {
                    "name": "submit",
                    "title": "Submit",
                    "type": "submit"
                }
            ]
        },
        {
            "name": "Page2",
            "inputs":
            [
                {
                    "title": "Catagory",
                    "name": "catagory",
                    "type": "radio",
                    "options":
                    [
                        {
                            "name": "Paper",
                            "value": "paper"
                        },
                        {
                            "name": "Letter",
                            "value": "letter"
                        }
                    ]
                },
                {
                    "title": "Title",
                    "name": "title",
                    "type": "text"
                },
                {
                    "title": "File",
                    "name": "file",
                    "type": "file",
                    "fileName": "?pages[0].inputs[0]"
                },
                {
                    "name": "submit",
                    "title": "Submit",
                    "type": "submit"
                }
            ]
        }
    ]
}

1 个答案:

答案 0 :(得分:0)

等于论证需要两个=符号 只有一个将设置变量,而不是比较它。

if ($config['pages'][$i]['inputs'][$j]['type'] == "radio")