Count length of array and return 1 if it only contains one element

前端 未结 4 1979
轻奢々
轻奢々 2020-12-09 15:33
$cars = \"bmw\",\"audi\",\"volvo\",\"vw\"
echo $cars.length

returns 4, but

$cars = \"bmw\"

returns 3 because it

4条回答
  •  难免孤独
    2020-12-09 16:26

    Maybe I am missing something (lots of many-upvotes-members answers here that seem to be looking at this different to I, which would seem implausible that I am correct), but length is not the correct terminology for counting something. Length is usually used to obtain what you are getting, and not what you are wanting.

    $cars.count should give you what you seem to be looking for.

提交回复
热议问题