开发者

Is this allowed in an array

I am trying to have a list of values separated by a comma like this:

$products = array( array( Title => "rose", 
                      Price => 1.25,1.31,1.54,1.39
                      //comma separated list
                      Type => dropdown
                    ),
               array( Title => "daisy", 
    开发者_如何学Python                  Price => 0.75,
                      Type => text_field,
                    ),
               array( Title => "orchid", 
                      Price => 1.15,
                      Type => text_field
                    )
             );

Is there a way i can items represent comma separated list in an array in php?.


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


If you have a comma separated list as a string, and want it represented as an array, you can use explode

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


Yes, You have to assign "1.25,1.31,1.54,1.39" in to ARRAY as string(by enclosed with "")

Like -

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


You cannot assign an array a comma-separated list because a pointer can only point to a memory not a structure. You can workaround by using another array or a string. When you want to to use a string you want to use the function explode() to separate the string.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜