【问题标题】:Obtaining key and values from a multidimentional array in a foreach loop (PHP/HTML)在 foreach 循环中从多维数组中获取键和值(PHP/HTML)
【发布时间】:2016-01-19 16:47:43
【问题描述】:

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

$propertiesMultiArray['propertyName'] = array(
    'formattedName' => 'formattedNameValue', 
    'example' => 'exampleValue', 
    'data' => 'dataValue');

我有一个表单,我想使用 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>"; 
    }

我希望 HTML 段类似于以下内容:

formattedNameValue : dataValue
e.g. exampleValue

其中 dataValue 位于输入文本字段中,$key 用作将该输入提交到表单的名称。本质上我想要 $key = "propertyName"。但是,它给出了以下错误:

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

如何从多维内部数组中获取信息,同时获取key?

【问题讨论】:

    标签: php html arrays multidimensional-array foreach


    【解决方案1】:

    有很多不同的方法来处理这个问题。一种选择是像这样使用complex string syntax

    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 将其与变量一起输出

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

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

    【讨论】:

    • 我在下面回答了自己,但您的 printf 选项是我学到的新东西,而且看起来很棒。我现在要切换到它。它会让事情变得非常干净。谢谢。
    • 谢谢!也感谢你告诉我我的 HTML 错误
    【解决方案2】:

    我总是这样写:

    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 中转义引号 (")。

    【讨论】:

      【解决方案3】:

      阅读variable parsing in strings 上的 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>"; 
      

      【讨论】:

        猜你喜欢
        • 2011-08-30
        • 2012-06-15
        • 1970-01-01
        • 2019-09-17
        • 2017-07-08
        • 1970-01-01
        • 2014-12-29
        • 2010-09-08
        相关资源
        最近更新 更多