Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/293.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
如何设置PHP数组?_Php - Fatal编程技术网

如何设置PHP数组?

如何设置PHP数组?,php,Php,基本上,我正在尝试创建一个数组,并将数组数据发送到我的电子邮件 我仍在学习PHP,对如何正确设置所有内容感到困惑 如果你有什么建议可以让我开始,我会非常感激 PHP <?php if (isset($_POST['submit'])) { $to = "test@mywebsite.com"; $subject = "New Order"; $name_field = $_POST['name']; $phone_field

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

我仍在学习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);
?>

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

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

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

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

在循环之前,最好定义要使用的变量:

$body = '';
foreach( $food as $key => $item) {
在循环中,您缺少一个分号:

$body.= $key." - ".$item ["how_many"]; 
                                     ^
$body = "Name: $name_field\nPhone: $phone_field\nKey: $key\nItem $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 -->

具体的问题是什么?你读了吗?问题到底是什么<代码>$\u POST
数据是否为空?您是否在任何地方都有
标记,或者只是表单元素?因此,
name
phone
字段在哪里?…或者至少使用字符串
'NO'
NO
可能是代码前面定义的常量。@Jocelyn在示例中没有指出。不要假设。根据OP判断,很明显我们可以假设NO的定义为:
define('YES','NO');定义(‘否’,是)
@Jocelyn我没有+1,但我会尽量记住+1这个评论。你完全正确,从技术上讲我是个伪君子。我认为你的回答涵盖了OP源代码中所有错误;)@MathieuImbert,尽管它是
name
属性,这让人非常困惑。这应该是一个
,或者
名称
属性的使用非常错误。您是对的,它可能是
值=“”
。但是谁知道
名称应该是什么…@MathieuImbert这个问题无法解决。我投票决定结束。
<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 -->