php str_getcsv array issue

后端 未结 3 815
清歌不尽
清歌不尽 2021-01-05 17:56

I am uploading a csv file and then parsing it using str_getcsv. All works great except that I need a way to cycle through them. Ideally, it\'d be great to have the array com

3条回答
  •  失恋的感觉
    2021-01-05 18:37

    Well, just count the number of items an entry has then make a loop inside a loop

    // loop on all items; $i+=$ITEM_SIZE go to the next item each outer loop
    // (10 elements for ITEM in Opening Post's case)
    
    // this for loops between all items
    for ($i=0;$i<=$ARRAY_LENGHT;$i+=10)
    {
        // this for loops between each item's elements
        for($c=1; $c<=10;$c++)
        {
            switch($c)
            {
                case 1:
                  $id = $array[$i+$c];
                  break;
    
                case 2:
                  $name = $array[$i+$c];
                  break;
    
                case 3:
                  $address = $array[$i+$c];
                  break;
    
                //etc to 10th element:
    
                case 10:
                  $other = $array[$i+$c];
                  break;
    
            }
        }
    
        // When the item is all done use the item
        // it as you need, example:
    
        echo "id: {$id}\nname: {$name}\naddress: {$adress}"; //etc
    
        // then unset($id,$name,$address, $etc);
        // for the next iteration of loop (next item)
    }
    

    That is what I just did and works very well.

提交回复
热议问题