Php 在U数组($variable[';key';])中,另一个[';key';])不工作
我不能上班了。它在我包含的文件中定义的函数中运行,然后从原始文件调用函数。。。这太复杂了,我将添加以下代码:Php 在U数组($variable[';key';])中,另一个[';key';])不工作,php,arrays,Php,Arrays,我不能上班了。它在我包含的文件中定义的函数中运行,然后从原始文件调用函数。。。这太复杂了,我将添加以下代码: index.php $results = $database->get_results($query); foreach ($results as $question) { echo '<div class="'.addQuestionClass($question).'"></div>'; } $sectionConfig = array(
index.php
$results = $database->get_results($query);
foreach ($results as $question) {
echo '<div class="'.addQuestionClass($question).'"></div>';
}
$sectionConfig = array(
'amount' => '20',
'types' => array('textarea', 'text', 'select', 'number'),
);
function addQuestionClass($question) {
if(is_array($question)) {
$order = 'order-'.$question['order'];
$id = ' question-'.$question['id'];
if(in_array($question['answer'], $sectionConfig['types'])) {
$answertype = ' type-'.$question['answer'];
echo 'true';
} else {
$answertype = null;
echo 'false';
}
return $answertype;
} else {
return;
}
}
问题代码在my addClass函数中:
在数组中($question['answer'],$sectionConfig['types'])
如果我运行相同的代码,粘贴$sectionConfig中的数组,如下图所示,它工作正常,但它永远无法识别上面的格式
这项工作:
在数组中($question['answer'],array('textarea','text','select','number'))
问题在于变量$sectionConfig
不在函数的范围内。您可以使用global
在函数范围内获取它,或将其作为变量传递:
function addQuestionClass($question) {
global $sectionConfig; // This gets your variable in the right scope.
if(is_array($question)) {
$order = 'order-'.$question['order'];
$id = ' question-'.$question['id'];
if(in_array($question['answer'], $sectionConfig['types'])) {
$answertype = ' type-'.$question['answer'];
echo 'true';
} else {
$answertype = null;
echo 'false';
}
return $answertype;
} else {
return;
}
}
问题是变量
$sectionConfig
不在函数的范围内。您可以使用global
在函数范围内获取它,或将其作为变量传递:
function addQuestionClass($question) {
global $sectionConfig; // This gets your variable in the right scope.
if(is_array($question)) {
$order = 'order-'.$question['order'];
$id = ' question-'.$question['id'];
if(in_array($question['answer'], $sectionConfig['types'])) {
$answertype = ' type-'.$question['answer'];
echo 'true';
} else {
$answertype = null;
echo 'false';
}
return $answertype;
} else {
return;
}
}
您正在函数中访问
$sectionConfig
。默认情况下,这是另一个作用域,函数中的代码不知道$sectionConfig
存在
您可以尝试以下方法:
$sectionConfig = array(
'amount' => '20',
'types' => array('textarea', 'text', 'select', 'number'),
);
$results = $database->get_results($query);
foreach ($results as $question) {
echo '<div class="'.addQuestionClass($question,$sectionConfig).'"></div>';
}
function addQuestionClass($question,$sectionConfig) {
if(is_array($question)) {
$order = 'order-'.$question['order'];
$id = ' question-'.$question['id'];
if(in_array($question['answer'], $sectionConfig['types'])) {
$answertype = ' type-'.$question['answer'];
echo 'true';
} else {
$answertype = null;
echo 'false';
}
return $answertype;
} else {
return;
}
}
$sectionConfig=array(
“金额”=>“20”,
'types'=>array('textarea','text','select','number'),
);
$results=$database->get_results($query);
foreach($结果作为$问题){
回声';
}
函数addQuestionClass($question$sectionConfig){
if(is_数组($question)){
$order='order-'。$PROJECT['order'];
$id='question-'。$question['id'];
if(在数组中($question['answer'],$sectionConfig['types'])){
$answertype='type-'。$question['answer'];
回声“真”;
}否则{
$answertype=null;
回声“假”;
}
返回$answertype;
}否则{
返回;
}
}
您正在访问函数内部的$sectionConfig
。默认情况下,这是另一个作用域,函数中的代码不知道$sectionConfig
存在
您可以尝试以下方法:
$sectionConfig = array(
'amount' => '20',
'types' => array('textarea', 'text', 'select', 'number'),
);
$results = $database->get_results($query);
foreach ($results as $question) {
echo '<div class="'.addQuestionClass($question,$sectionConfig).'"></div>';
}
function addQuestionClass($question,$sectionConfig) {
if(is_array($question)) {
$order = 'order-'.$question['order'];
$id = ' question-'.$question['id'];
if(in_array($question['answer'], $sectionConfig['types'])) {
$answertype = ' type-'.$question['answer'];
echo 'true';
} else {
$answertype = null;
echo 'false';
}
return $answertype;
} else {
return;
}
}
$sectionConfig=array(
“金额”=>“20”,
'types'=>array('textarea','text','select','number'),
);
$results=$database->get_results($query);
foreach($结果作为$问题){
回声';
}
函数addQuestionClass($question$sectionConfig){
if(is_数组($question)){
$order='order-'。$PROJECT['order'];
$id='question-'。$question['id'];
if(在数组中($question['answer'],$sectionConfig['types'])){
$answertype='type-'。$question['answer'];
回声“真”;
}否则{
$answertype=null;
回声“假”;
}
返回$answertype;
}否则{
返回;
}
}
您的函数addQuestionClass
根本无法访问$sectionConfig
数组。如果它甚至不能读取值,它如何工作?您的函数addQuestionClass
根本无法访问$sectionConfig
数组。如果它甚至不能读取值,它如何工作?您最好不要使用全局
,有关更多信息,请参阅。相反,只需像我的回答中那样传递数组。最好不要使用global
,有关更多信息,请参阅。我觉得自己像个白痴,在这方面有很多问题,谢谢。我觉得自己像个白痴,在这方面有很多问题,谢谢