如何设置PHP数组


How to setup a PHP array?

基本上,我正在尝试创建一个数组,并将数组数据发送到我的电子邮件中。

我仍在学习PHP,对如何正确设置一切感到困惑。

如果你有什么建议可以让我开始,我真的很感激。

PHP

<?php
if (isset($_POST['submit'])) {
    $to          = "test@mywebsite.com";
    $subject     = "New Order";
    $name_field  = $_POST['name'];
    $phone_field = $_POST['phone'];

    foreach ($food as $key => $item) {
        $body.= $key." - ".$item ["how_many"]
    }

    $food = array(
    'mexican_torta' => array('how_many' => 2, 'customize' => NO),
    'fish_sandwich' => array('how_many' => 0, 'customize' => 0)
    )
    );
    echo $food['mexican_torta']['how_many'];
}
$body = "Name: $name_field'nPhone: $phone_field'nKey: $key'nItem $item"
echo "Data has been submitted to $to!";
mail($to, $subject, $body);
?>

HTML

<div class ="item">
    <img style="float:left; margin-right:15px; border:1px Solid #000; width:200px; height:155px;" src="images/mexicantortas.jpg">
    <h1>Mexican Torta - $8.50</h1>
    <h2>How Many? <font color="#999999">Ex: 1, 2, 3...?</font></h2> 
    <input type='text' name='food[mexican_torta][how_many]'>
    <h3>Customize It? <font color="#999999">Ex: No Lettuce, Extra Cheese...</font></h3> 
    <input type='text' name='food[mexican_torta][customize]'>
</div><!-- ITEM_LEFT -->
<div class ="item">
    <img style="float:left; margin-right:15px; border:1px Solid #000; width:200px; height:155px;" src="images/fishsandwich.jpg">
    <h1>Fish Sandwich - $8.50</h1>
    <h2>How Many? <font color="#999999">Ex: 1, 2, 3...?</font></h2> 
    <input type='text' name='food[fish_sandwich][how_many]'>
    <h3>Customize It? <font color="#999999">Ex: No Lettuce, Extra Cheese...</font></h3> 
    <input type='text' name='food[fish_sandwich][customize]'>
</div><!-- ITEM_LEFT -->

$food是在循环之后而不是在循环之前定义的,并且有一个额外的尾部):

$food = array( 
    // NO is also not defined as of yet, see Matheiu's answer.
    'mexican_torta' => array('how_many' => 2, 'customize' => NO), 
    'fish_sandwich' => array('how_many' => 0, 'customize' => 0)
);

在循环之前,定义将要使用的变量是一种很好的做法:

$body = '';
foreach( $food as $key => $item) {

在循环中,您缺少一个分号:

$body.= $key." - ".$item ["how_many"]; 
                                     ^

你的身体陈述缺少一个分号:

$body = "Name: $name_field'nPhone: $phone_field'nKey: $key'nItem $item";
                                                                       ^

最后,最后一条语句覆盖了$body,因此循环没有执行任何操作。考虑一下这样的东西:

$body .= "Name: $name_field'nPhone: $phone_field'nKey: $key'nItem $item";

尽管要注意,$key$item将指向$food数组中的最后一个元素,而$item是一个数组,因此无法正确转换为字符串。

我将专注于您的HTML:

<div class ="item">
    <img style="float:left; margin-right:15px; border:1px Solid #000; width:200px; height:155px;" src="images/mexicantortas.jpg">
    <h1>Mexican Torta - $8.50</h1>
    <h2>How Many? <font color="#999999">Ex: 1, 2, 3...?</font></h2> 
    <input type='text' name='food[mexican_torta][how_many]'>
    <h3>Customize It? <font color="#999999">Ex: No Lettuce, Extra Cheese...</font></h3> 
    <input type='text' name='food[mexican_torta][customize]'>
</div><!-- ITEM_LEFT -->

这看起来像是您试图访问$food,但没有在PHP标记中进行访问。如果你想把你的代码解析成PHP,你必须告诉服务器它必须把它解析成PHP:

<div class ="item">
    <img style="float:left; margin-right:15px; border:1px Solid #000; width:200px; height:155px;" src="images/mexicantortas.jpg">
    <h1>Mexican Torta - $8.50</h1>
    <h2>How Many? <font color="#999999">Ex: 1, 2, 3...?</font></h2> 
    <input type='text' name='<?php echo $food['mexican_torta']['how_many']; ?>'> <!-- major difference here -->
    <h3>Customize It? <font color="#999999">Ex: No Lettuce, Extra Cheese...</font></h3> 
    <input type='text' name='<?php echo $food['mexican_torta']['customize']; ?>'> <!-- major difference here -->
</div><!-- ITEM_LEFT -->

创建数组时出现解析错误。这可能是它不起作用的原因:

$food = array(
  'mexican_torta' => array('how_many' => 2, 'customize' => NO),
  'fish_sandwich' => array('how_many' => 0, 'customize' => 0)
);

并且未定义CCD_ 9。您应该保持一致,并将其更改为0