如何设置PHP数组?

时间:2012-08-22 18:10:51

标签: php

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

我还在学习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 -->

3 个答案:

答案 0 :(得分:2)

$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是一个数组,因此它无法正确转换为一个字符串。

答案 1 :(得分:2)

我将专注于您的 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 -->

答案 2 :(得分:1)

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

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

NO未定义。您应保持一致并将其更改为0