Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何生成.json数组';什么是PHP?_Php_Arrays_Json_For Loop_Foreach - Fatal编程技术网

如何生成.json数组';什么是PHP?

如何生成.json数组';什么是PHP?,php,arrays,json,for-loop,foreach,Php,Arrays,Json,For Loop,Foreach,我正在尝试制作一个包含一系列问题的表单,这些问题可以添加到radioBox、复选框等中。我制作了一个.json文件,其中包含类似这样的问题和可能的答案(这只是文件的一小部分): 我使用了一个类,在这个类中我有几个函数来生成数组,可以在我的常规.php页面上使用。我使用下面的代码提出了一系列问题,这些问题很有效: $questions = []; foreach($json['questions'] as $key => $value){ $this->questio

我正在尝试制作一个包含一系列问题的表单,这些问题可以添加到radioBox、复选框等中。我制作了一个.json文件,其中包含类似这样的问题和可能的答案(这只是文件的一小部分):

我使用了一个类,在这个类中我有几个函数来生成数组,可以在我的常规.php页面上使用。我使用下面的代码提出了一系列问题,这些问题很有效:

$questions = [];

foreach($json['questions'] as $key => $value){
        $this->questions[] = $value['question'];
}
现在我可以用问题[0]来得到第一个问题,第二个问题是问题[1],这真的很好。我想做的是创建一个数组,其中包含每个问题的所有答案,这样我就可以对这些答案做类似的操作。理想情况下,它看起来像这样:

array:
    arrayQuestion1Answers:
        string: answer1
        string: answer2
        string: answer3
    arrayQuestion2Answers:
        string: answer1
        string: answer2
        string: answer3
$questions = json_decode($json);
$answers_to_question_1 = $questions[0]['answers'] ?? []; // ?? [] means: if not set, make it an empty array. This is the null coalesce feature, available in PHP 7.0 and higher. See http://php.net/manual/en/migration70.new-features.php.
这样,我就可以做一些类似于arrayQuestion1[0]的事情,从第一个问题中得到第一个答案,而arrayQuestion2[2]从第二个问题中得到第三个答案


谢谢你阅读我的长(可能是愚蠢的)问题,希望你能帮助我

您说您已经有了将JSOn转换为PHP数组的函数。你退房过吗?这是一个本机PHP函数,用于解码JSON。失败时返回false

此外:为什么要为JSON中的
问题
对象编制索引?您不妨键入:

{
    "questions": [
        {"somekey": "this_is_question_0"},
        {"somekey": "this_is_question_1"}
    ]
}
这样,它们就会自动编入索引

要回答您的实际问题,如果您使用我上面描述的内容,您只需访问以下问题的答案:

array:
    arrayQuestion1Answers:
        string: answer1
        string: answer2
        string: answer3
    arrayQuestion2Answers:
        string: answer1
        string: answer2
        string: answer3
$questions = json_decode($json);
$answers_to_question_1 = $questions[0]['answers'] ?? []; // ?? [] means: if not set, make it an empty array. This is the null coalesce feature, available in PHP 7.0 and higher. See http://php.net/manual/en/migration70.new-features.php.
试试这个

$json = '{
    "questions": {
        "0": {
            "question":"How many products?","answers":["Less than 1000", "More than 1000", "More than 10000"],"score":[1, 2, 3],"info":"This is additional information for question 1"
        }
        ,"1":{
            "question":"How many websites?","answers":["One", "Two", "Three"],"score":[1, 2, 3],"info":"This is additional information for question 2"
        }
    }
}
';
$questions = json_decode($json,true);
$question = $questions['questions'];
foreach($question as $key => $que):
    $answer['arrayQuestion'.$key.'Answers'] = $que['answers'];
endforeach;
echo '<pre>';
print_r($answer);
$json=”{
“问题”:{
"0": {
“问题”:“有多少种产品?”,“答案”:[“少于1000”、“超过1000”、“超过10000”],“分数”:[1,2,3],“信息”:“这是问题1的附加信息”
}
,"1":{
“问题”:“有多少个网站?”,“答案”:[“一”、“二”、“三”],“分数”:[1,2,3],“信息”:“这是问题2的补充信息”
}
}
}
';
$questions=json_decode($json,true);
$question=$questions['questions'];
foreach($key=>$que的问题):
$answer['arrayQuestion'.$key.Answers']=$que['Answers'];
endforeach;
回声';
打印(答案);

仅使用json解码数组有什么问题?“第一个问题的第一个答案”应该是
$json['questions'][0]['answers'][0]
,这是真的,但我想知道是否有可能制作一个数组。如果你的json是有效的,这将是一个很好的例子。好的,wiseguy先生,我知道我不能在json中键入注释,否则我会键入“无效JSON,这只是一个例子”,但我会更新答案,因为你在技术上是正确的。感谢你为那些可能不认识无效JSON的人指出这一点。