我正在尝试使用逗号分隔的值列表:
$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中的数组中用逗号分隔列表?
答案 0 :(得分: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
)
);
答案 1 :(得分:3)
如果您将逗号分隔列表作为字符串,并希望将其表示为数组,则可以使用explode
$myString = "1,2,3,4,5";
$myArray = explode(',',$myString);
答案 2 :(得分:0)
是的,您必须将“1.25,1.31,1.54,1.39”分配给ARRAY作为字符串(附带“”)
喜欢 -
$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
)
);
答案 3 :(得分:0)
您不能为数组指定逗号分隔列表,因为指针只能指向内存而不是结构。您可以使用另一个数组或字符串来解决此问题。如果要使用字符串,则需要使用函数explode()来分隔字符串。