Exploding a string twice

前端 未结 3 1794
闹比i
闹比i 2021-01-24 04:17

I have a string composed like this:

87||1|nuovo#88||4|#209|||#89|||#41||1|#5|||#3||1|116#20|||#13||3|#148|||

The pattern is:

相关标签:
3条回答
  • 2021-01-24 05:08

    Sight change to Jiri's code.

    Replace this line

    list($id, $mq, $qr, $tipo) = explode('|', $i);
    

    with

    list($id[], $mq[], $qr[], $tipo[]) = explode('|', $i);
    
    0 讨论(0)
  • 2021-01-24 05:14

    Try following code

        $str = "87||1|nuovo#88||4|#209|||#89|||#41||1|#5|||#3||1|116#20|||#13||3|#148|||#36|||91#29|||68";
    $caratteristica = explode("#",$str);
    
    foreach ($caratteristica as $value) {
     list($id[], $mq[], $qr[], $tipo[]) = explode('|', $value);
    }
    
    // For check the result
    echo "<pre/>";
    print_r($id);
    
    print_r($mq);
    
    print_r($qr);
    
    print_r($tipo);
    

    I got following answer

        Array
    (
        [0] => 87
        [1] => 88
        [2] => 209
        [3] => 89
        [4] => 41
        [5] => 5
        [6] => 3
        [7] => 20
        [8] => 13
        [9] => 148
        [10] => 36
        [11] => 29
    )
    Array
    (
        [0] => 
        [1] => 
        [2] => 
        [3] => 
        [4] => 
        [5] => 
        [6] => 
        [7] => 
        [8] => 
        [9] => 
        [10] => 
        [11] => 
    )
    Array
    (
        [0] => 1
        [1] => 4
        [2] => 
        [3] => 
        [4] => 1
        [5] => 
        [6] => 1
        [7] => 
        [8] => 3
        [9] => 
        [10] => 
        [11] => 
    )
    Array
    (
        [0] => nuovo
        [1] => 
        [2] => 
        [3] => 
        [4] => 
        [5] => 
        [6] => 116
        [7] => 
        [8] => 
        [9] => 
        [10] => 91
        [11] => 68
    )
    
    0 讨论(0)
  • 2021-01-24 05:23
    <?php
    $input = '87||1|nuovo#88||4|#209|||#89|||#41||1|#5|||#3||1|116#20|||#13||3|#148|||';
    $items = explode('#', $input);
    $result = [];
    # or $id = $mq = $qr = $tipo = [];
    foreach ($items as $i) {
      list($id, $mq, $qr, $tipo) = explode('|', $i);
      $result[] = ['id' => $id, 'mq' => $mq, 'qr' => $qr, 'tipo' => $tipo];
      # or $id[] = $id; $mq[] = $mq; $qr[] = $qr; $tipo[] = $tipo;
    }
    
    0 讨论(0)
提交回复
热议问题