Find numeric value in csv

后端 未结 2 344
耶瑟儿~
耶瑟儿~ 2021-01-27 11:54

I need to check if a particular numeric id is in a given string or comma separated values.

$myVal = 20;

String could contain just a single valu

相关标签:
2条回答
  • 2021-01-27 12:19

    You could use explode to split the string into an array. Something like:

    $parts = explode(',', $str);
    

    Then you could use in_array on that.

    If you're reading a file, take a look at fgetcsv or str_getcsv.

    0 讨论(0)
  • 2021-01-27 12:30

    Use in_array and explode:

    $str = '10, 20, 30, 40';
    if(in_array(20, explode(', ', $str))) echo 'In Array';
    else echo 'Not In Array';
    

    Note that I am using the numeric 20 because it will equal the ' 20' value. If I used '20', it will not match ' 20'.

    0 讨论(0)
提交回复
热议问题