Trying to parse a fixed-width text file into an array for each line.
All I'm getting is an empty array with 10 empty elements when I should be getting
an array with 10 arrays of 5 elements.
Note: I'm ignoring the last three columns.

I also have to manipulate the $pn variable, that is why I'm not just using the array_values function as input to the $fields_order array.

$fp=@fopen($fileName,"r") or die ("Can not open file.");
while ($line=@fgets($fp)){
  list($pn, $apn, $cc, $q, $desc, $x, $y, $z)=array_values(unpack("A25pn/A11apn/A11cc/A10q/A13desc/A13x/A11y/A18z",$line));
  $fields_order[] = implode(',', $pn.','.$apn.','.strtoupper($cc).','.$q.','.$desc);
}      
print_r($fields_order);

Data is as follows:

5701C60-530136-9         605301369           NS         2    HANDLE              EABE        1        000000000Y
5701CA35AIWBK                                NS         4    HOOD                EAMISC      1244132  000000000Y
5701C77982-002           77982002            SV         1    STRAP               EAPI        5        000000000Y
5701C101-306                                 NS         1    PLATE               EACS        1241908  000000000Y
5701C763-731             763731              NE         3    WASHER-MIN ORD 100 EEAPI        33       000000000Y
5701C763-731             763731              NS         2    WASHER-MIN ORD 100 EEAPI        33       000000000Y
5701C2524648-G           2524298-10          AR         1    SERVO FUEL INJECT.  EAMISC      34       000000000Y
5701C2524648-G           RSA10ED1            AR         1    SERVO FUEL INJECT.  EAMISC      34       000000000Y
5701C2524648-G           2524648G            AR         1    SERVO FUEL INJECT.  EAMISC      34       000000000Y
5701CLBV11S025-S90                           AR         1    FITTING             EAMISC      1244126  000000000Y

    implode takes two arguments. 1st is a string acting as delimiter, the second is an array (which you do not provide in your code)

    	// creating a string - this is what I believe you try to do with your code
    	$fields_order_str[] = "$pn,$apn,".strtoupper($cc).",$q,$desc";
    
    // example using implode
    // creating an array - this is how I interpret your wish: 10 arrays with 5 elems each
    $fields_order_arr[] = array($pn,$apn,strtoupper($cc),$q,$desc);
    // creating a string from an array - identical to $fields_order_str
    $fields_order_str_from_array[] = implode(',',$fields_order_arr[count($fields_order_arr)-1]);
    }
    
    print_r($fields_order_arr);
    print_r($fields_order_str);
    print_r($fields_order_str_from_array);
    

      This here works exactly like I want:

      $fields_order[] = array($pn,$apn,strtoupper($cc),$q,$desc);

      BTW, I DID try setting the input to implode as an array and it still returned empty array.

        Write a Reply...