Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/codeigniter/3.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
从foreach循环中的多维数组中获取键和值(PHP/HTML)_Php_Html_Arrays_Multidimensional Array_Foreach - Fatal编程技术网

从foreach循环中的多维数组中获取键和值(PHP/HTML)

从foreach循环中的多维数组中获取键和值(PHP/HTML),php,html,arrays,multidimensional-array,foreach,Php,Html,Arrays,Multidimensional Array,Foreach,我有一个多维数组,其中每个条目如下所示: $propertiesMultiArray['propertyName'] = array( 'formattedName' => 'formattedNameValue', 'example' => 'exampleValue', 'data' => 'dataValue'); formattedNameValue : dataValue e.g. exampleValue 我有一个表单,我想使用for

我有一个多维数组,其中每个条目如下所示:

$propertiesMultiArray['propertyName'] = array(
    'formattedName' => 'formattedNameValue', 
    'example' => 'exampleValue', 
    'data' => 'dataValue');
formattedNameValue : dataValue
e.g. exampleValue
我有一个表单,我想使用foreach循环来填充值和输入字段特征,使用外部数组中的键以及内部数组中存储的不同信息。所有值都将用作字符串。到目前为止,我已经

foreach($propertiesMultiArray as $key => $propertyArray){
    echo "<p>$propertyArray['formattedName'] : " .
    "<input type=\"text\" name=\"$key\" size=\"35\" value=\"$propertyArray['data']\">" .
    "<p class=\"example\"> e.g. $propertyArray['example'] </p>" .
    "<br><br></p>"; 
    }
其中dataValue位于输入文本字段中,$key用作将该输入提交到表单的名称。基本上我想要$key=propertyName。但是,它给出了以下错误:

syntax error, unexpected '' (T_ENCAPSED_AND_WHITESPACE), expecting identifier (T_STRING) or variable (T_VARIABLE) or number (T_NUM_STRING)

如何从多维数组的内部数组访问信息,但同时获取密钥?

有很多不同的方法来处理此问题。一种选择是使用如下所示的:

foreach($propertiesMultiArray as $key => $propertyArray) {
    echo "<p>{$propertyArray['formattedName']} : " .
    "<input type=\"text\" name=\"$key\" size=\"35\" value=\"{$propertyArray['data']}\">" .
    "<p class=\"example\"> e.g. {$propertyArray['example']} </p>" .
    "<br><br></p>"; 
    }
foreach($propertiesMultiArray as $key => $propertyArray){
echo '<p>'.$propertyArray['formattedName'].' : ' .  '<input type="text" name="$key" size="35" value="'.$propertyArray['data'].'">'.
'<p class="example"> e.g.'. $propertyArray['example'] .'</p>' .
'<br><br></p>'; 
}
另一个选项是将HTML设置为格式字符串,并使用


顺便说一下,我在编写printf示例时注意到,您的HTML在一个段落中有一个段落。我认为这不是有效的HTML。

有很多不同的方法来处理这个问题。一种选择是使用如下所示的:

foreach($propertiesMultiArray as $key => $propertyArray) {
    echo "<p>{$propertyArray['formattedName']} : " .
    "<input type=\"text\" name=\"$key\" size=\"35\" value=\"{$propertyArray['data']}\">" .
    "<p class=\"example\"> e.g. {$propertyArray['example']} </p>" .
    "<br><br></p>"; 
    }
foreach($propertiesMultiArray as $key => $propertyArray){
echo '<p>'.$propertyArray['formattedName'].' : ' .  '<input type="text" name="$key" size="35" value="'.$propertyArray['data'].'">'.
'<p class="example"> e.g.'. $propertyArray['example'] .'</p>' .
'<br><br></p>'; 
}
另一个选项是将HTML设置为格式字符串,并使用


顺便说一下,我在编写printf示例时注意到,您的HTML在一个段落中有一个段落。我认为这不是有效的HTML。

请阅读上的PHP文档。当您将数组元素嵌入双引号字符串或here doc中时,有两种写入方法;简单语法:

"...$array[index]..."
索引周围没有引号,或语法复杂:

"...{array['index']}..."
表达式周围使用大括号,索引使用常规语法。您的错误是因为您使用了第一个语法,但在索引周围加了引号

因此,它应该是:

echo "<p>$propertyArray['formattedName'] : " .
"<input type=\"text\" name=\"$key\" size=\"35\" value=\"{$propertyArray['data']}\">" .
"<p class=\"example\"> e.g. {$propertyArray['example']} </p>" .
"<br><br></p>"; 

阅读上的PHP文档。当您将数组元素嵌入双引号字符串或here doc中时,有两种写入方法;简单语法:

"...$array[index]..."
索引周围没有引号,或语法复杂:

"...{array['index']}..."
表达式周围使用大括号,索引使用常规语法。您的错误是因为您使用了第一个语法,但在索引周围加了引号

因此,它应该是:

echo "<p>$propertyArray['formattedName'] : " .
"<input type=\"text\" name=\"$key\" size=\"35\" value=\"{$propertyArray['data']}\">" .
"<p class=\"example\"> e.g. {$propertyArray['example']} </p>" .
"<br><br></p>"; 

我总是这样写:

foreach($propertiesMultiArray as $key => $propertyArray) {
    echo "<p>{$propertyArray['formattedName']} : " .
    "<input type=\"text\" name=\"$key\" size=\"35\" value=\"{$propertyArray['data']}\">" .
    "<p class=\"example\"> e.g. {$propertyArray['example']} </p>" .
    "<br><br></p>"; 
    }
foreach($propertiesMultiArray as $key => $propertyArray){
echo '<p>'.$propertyArray['formattedName'].' : ' .  '<input type="text" name="$key" size="35" value="'.$propertyArray['data'].'">'.
'<p class="example"> e.g.'. $propertyArray['example'] .'</p>' .
'<br><br></p>'; 
}

它还可以避免在整个HTML中转义引号。

我总是这样写:

foreach($propertiesMultiArray as $key => $propertyArray) {
    echo "<p>{$propertyArray['formattedName']} : " .
    "<input type=\"text\" name=\"$key\" size=\"35\" value=\"{$propertyArray['data']}\">" .
    "<p class=\"example\"> e.g. {$propertyArray['example']} </p>" .
    "<br><br></p>"; 
    }
foreach($propertiesMultiArray as $key => $propertyArray){
echo '<p>'.$propertyArray['formattedName'].' : ' .  '<input type="text" name="$key" size="35" value="'.$propertyArray['data'].'">'.
'<p class="example"> e.g.'. $propertyArray['example'] .'</p>' .
'<br><br></p>'; 
}

它还可以避免您在整个HTML中逃过引号。

我在下面回答了自己的问题,但您的printf选项是我学到的新东西,看起来很棒。我现在要换个话题。这会让事情变得很干净。谢谢,谢谢!谢谢你告诉我我的HTML错误,我在下面回答了自己,但是你的printf选项是我学到的新东西,看起来很棒。我现在要换个话题。这会让事情变得很干净。谢谢,谢谢!谢谢你告诉我我的HTML错误