du90093662774150 2014-01-26 03:17
浏览 282
已采纳

PHP:将逗号分隔值转换为整数数组

I've got a string of comma separated values: 1,2,3,4,5,6,7,8,9,10,11,12,13

How can I get rid of the comma, turn each value into an integer and end up with a PHP array of these numbers?

$number_array = [1, 2, 3, 4 ....., 13]; - something like that?

  • 写回答

1条回答 默认 最新

  • douying3251 2014-01-26 03:19
    关注

    Make use of explode()

    <?php
    $str='1,2,3,4,5,6,7,8,9,10,11,12,13';
    $arr = explode(',',$str);
    $intarr = array_map('intval',$arr);
    var_dump($intarr);
    

    Well, a one-liner.

    var_dump(array_map('intval',explode(',','1,2,3,4,5,6,7,8,9,10,11,12,13')));
    

    OUTPUT :

    array(13) {
      [0]=>
      int(1)
      [1]=>
      int(2)
      .
      .   // Lines Skipped
      .
      int(12)
      [12]=>
      int(13)
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?