2011-04-20 107 views
1

我想有一个逗号这样分隔值的列表:难道这允许在一个阵列

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

有没有一种方法可以让我代表项目逗号PHP中的数组分隔列表?

回答

3
$products = array(array(Title => "rose", 
         Price => array(1.25,1.31,1.54,1.39) 
         //comma separated list 
         Type => dropdown 
        ), 
       array(Title => "daisy", 
         Price => 0.75, 
         Type => text_field, 
        ), 
       array(Title => "orchid", 
         Price => 1.15, 
         Type => text_field 
        ) 
      ); 
0

是的,你必须在数组赋值 “1.25,1.31,1.54,1.39” 的字符串(由附带 “”)

像 -

$products = array(array(Title => "rose", 
         Price => "1.25,1.31,1.54,1.39", 
         //comma separated list 
         Type => "dropdown" 
        ), 
       array(Title => "daisy", 
         Price => 0.75, 
         Type => text_field, 
        ), 
       array(Title => "orchid", 
         Price => 1.15, 
         Type => text_field 
        ) 
      ); 
+0

我现在越来越注意:使用未定义的常量标题 - 假定在“标题” C:\ wamp \ www \ serializer.php在第2行。 – Gandalf 2011-04-20 12:32:27

+0

如果数组不是整数,那么必须将数组索引/键编写为字符串,例如,所有数组键都是字符串。所以你必须把它们写成字符串。标题为“标题”,价格为“价格”等 – mahadeb 2011-04-20 17:05:40

0

您不能分配数组是逗号分隔的列表,因为指针只能指向内存而不是结构。您可以通过使用另一个数组或字符串来解决方法。当你想使用一个字符串时,你想使用explode()函数来分隔字符串。

3

如果你有一个逗号分隔的列表作为一个字符串,并希望它表现为一个数组,你可以使用explode

$myString = "1,2,3,4,5"; 
$myArray = explode(',',$myString);