Gandalf
Gandalf

Reputation: 13693

odd errors in php array

I am trying to json encode an array,it does encode but i get lots of errors:

$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);

I get the following errors.

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

Upvotes: 3

Views: 2542

Answers (6)

Karl
Karl

Reputation: 11

I came across the exact same problem and after staring at the PHP5 manual page for arrays it eventually clicked. Here is what I found out:

This line

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

will cause that notice to show. I also use this line in my code

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

When I wrap the array key in single quotes in each line as such

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

I get a parse error on the second line, but the first line appears to be fine. Looking at https://www.php.net/manual/en/function.array.php and there Example #4 the answer is right there. When accessing array values within a double quoted string you have to enclose the array value construct in curly braces (mustaches). As it turns out, this is how it is correct:

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

Oddly, this works as well without throwing parse errors or notices...welcome to the logic of PHP:

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

Rather bizarre that both lines work fine and generate the expected result. While the no-single-quote-no-curly-braces notation works I do suggest to go with how it seems to be correct and use curly braces and single quotes within a string. In any case, reading the docs and mulling over it some time fixed it for me. And yes, sadly, it's all there right in the manual!

Upvotes: 1

Cyril Deba
Cyril Deba

Reputation: 1210

you must use quotation mark for STRING keys in arrays. Your code with changes is shown below:

<?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);

Additional information about arrays in php you will find here PHP: Arrays

Upvotes: 2

Aaria Carter-Weir
Aaria Carter-Weir

Reputation: 1679

You might be confusing javascripts object notation syntax with PHP here, like the other answers have suggested, wrapping the array keys in quotes (so that they're passed in as strings) will sort out your problem.

It might be worth reading up on PHP Constants to better understand the error message you've been given: http://php.net/manual/en/language.constants.php

Upvotes: 1

bensiu
bensiu

Reputation: 25564

    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"
                )
         );

Upvotes: 1

Mild Fuzz
Mild Fuzz

Reputation: 30691

put quotes around the array key names

$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);

Upvotes: 1

Karl Andrew
Karl Andrew

Reputation: 1555

You need to quote the keys. Without quotes, they're constants. The interpreter is guessing what you mean, but you should change it to avoid the notice.

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

Upvotes: 10

Related Questions