php数组中的奇怪错误

时间:2011-04-20 12:55:34

标签: php arrays multidimensional-array

我正在尝试json编码一个数组,它确实编码但我得到了很多错误:

$products = array( array( Title => "rose", 
                      Price => "1.25,1.31,1.54,1.39",
                      Type => "dropdown"
                    ),
               array( Title => "daisy", 
                      Price => "0.75",
                      Type => "text_field",
                    ),
               array( Title => "orchid", 
                      Price => "1.15",
                      Type => "text_field"
                    )
             );
echo json_encode($products);

我收到以下错误。

Notice: Use of undefined constant Title - assumed 'Title' in C:\wamp\www\serializer.php on line 2

Notice: Use of undefined constant Price - assumed 'Price' in C:\wamp\www\serializer.php on line 3

Notice: Use of undefined constant Type - assumed 'Type' in C:\wamp\www\serializer.php on line 4

Notice: Use of undefined constant Title - assumed 'Title' in C:\wamp\www\serializer.php on line 6

Notice: Use of undefined constant Price - assumed 'Price' in C:\wamp\www\serializer.php on line 7

Notice: Use of undefined constant Type - assumed 'Type' in C:\wamp\www\serializer.php on line 8

Notice: Use of undefined constant Title - assumed 'Title' in C:\wamp\www\serializer.php on line 10

Notice: Use of undefined constant Price - assumed 'Price' in C:\wamp\www\serializer.php on line 11

Notice: Use of undefined constant Type - assumed 'Type' in C:\wamp\www\serializer.php on line 12

6 个答案:

答案 0 :(得分:9)

您需要引用密钥。没有引号,它们就是常量。口译员猜测你的意思,但你应该改变它以避免通知。

$products = array( array( "Title" => "rose", 
                  "Price" => "1.25,1.31,1.54,1.39",
                  "Type" => "dropdown"
                ),

答案 1 :(得分:2)

您必须在数组中使用STRING键的引号。您的更改代码如下所示:

<?php $products = array( array( 'Title' => "rose", 
                  'Price' => "1.25,1.31,1.54,1.39",
                  'Type' => "dropdown"
                ),
           array( 'Title' => "daisy", 
                  'Price' => "0.75",
                  'Type' => "text_field",
                ),
           array( 'Title' => "orchid", 
                  'Price' => "1.15",
                  'Type' => "text_field"
                )
         ); echo json_encode($products);

有关php中数组的其他信息,请参阅PHP: Arrays

答案 2 :(得分:1)

在数组键名称周围加上引号

$products = array( array( 'Title' => "rose", 
                      'Price' => "1.25,1.31,1.54,1.39",
                      'Type' => "dropdown"
                    ),
               array( 'Title' => "daisy", 
                      'Price' => "0.75",
                      'Type' => "text_field",
                    ),
               array( 'Title' => "orchid", 
                      'Price' => "1.15",
                      'Type' => "text_field"
                    )
             );
echo json_encode($products);

答案 3 :(得分:1)

    array( array( 'Title' => "rose", 
                  'Price' => "1.25,1.31,1.54,1.39",
                  'Type' => "dropdown"
                ),
           array( 'Title' => "daisy", 
                  'Price' => "0.75",
                  'Type' => "text_field",
                ),
           array( 'Title' => "orchid", 
                  'Price' => "1.15",
                  'Type' => "text_field"
                )
         );

答案 4 :(得分:1)

你可能会在这里混淆javascripts对象表示法语法,就像其他答案所建议的那样,将数组键包装在引号中(以便它们以字符串形式传入)将解决你的问题。

可能值得阅读PHP常量以更好地理解您给出的错误消息:http://php.net/manual/en/language.constants.php

答案 5 :(得分:1)

我遇到了完全相同的问题,在盯着数组的PHP5手册页后,它最终点击了。这是我发现的:

这一行

If ($showallresult[Composer] == "") $showallresult[Composer] = "?";

将导致该通知显示。 我也在我的代码中使用这一行

print ("<TD ALIGN=CENTER VALIGN=TOP>$showallresult[Composer]</TD>\n");

当我将数组键包装在每行中的单引号中时

If ($showallresult['Composer'] == "") $showallresult['Composer'] = "?";
print ("<TD ALIGN=CENTER VALIGN=TOP>$showallresult['Composer']</TD>\n");

我在第二行得到一个解析错误,但第一行似乎没问题。看http://us2.php.net/manual/en/function.array.php和示例#4,答案就在那里。在双引号字符串中访问数组值时,必须将数组值构造括在花括号(mustaches)中。事实证明,这是正确的:

If ($showallresult['Composer'] == "") $showallresult['Composer'] = "?";
print ("<TD ALIGN=CENTER VALIGN=TOP>{$showallresult['Composer']}</TD>\n");

奇怪的是,这也可以解决,无需抛出解析错误或通知......欢迎使用PHP的逻辑:

If ($showallresult['Composer'] == "") $showallresult['Composer'] = "?";
print ("<TD ALIGN=CENTER VALIGN=TOP>$showallresult[Composer]</TD>\n");

相当奇怪的是两条线路都能很好地工作并产生预期的结果。虽然没有单引号 - 没有花括号的符号可行,但我建议使用它看起来是正确的,并在字符串中使用花括号和单引号。 在任何情况下,阅读文档并仔细考虑它有一段时间为我修复它。是的,遗憾的是,手册中的所有内容都是正确的!