phparrayscsvcsvtoarray

Inside of array's arrays elements


So this might be a easy question but I cant figure it ok, how would I get to the data inside of array's array? I want to grab the last two comma-separated values from the second element in the array's array if that makes any sense.

My array looks like this:

 Array
(
[0] => Array
    (
        [0] => Print Date
        [1] => Cost
        [2] => Recipient
        [3] => Status
        [4] => Tracking #/Insurance ID
        [5] => Date Delivered
        [6] => Carrier
        [7] => Class Service
        [8] => Special Services
        [9] => Insured Value
        [10] => Cost Code
        [11] => Weight
        [12] => Mail Date
        [13] => Refund Type
    )

[1] => Array
    (
        [0] => 1/20/2016
        [1] => $8.15
        [2] => Justin De Los Polive, PO BOX 2353, BLANCO, TX 78606-1232
        [3] => Printed
        [4] => ="9405511"
        [5] => 
        [6] => USPS
        [7] => Priority Mail (R)
        [8] => USPS Tracking
        [9] => 
        [10] => 
        [11] => 1lb 8oz
        [12] => 1/20/2016
        [13] => E-refund
    )

[2] => Array
    (
        [0] => 1/20/2016
        [1] => $8.15
        [2] => Kist Benings, PO BOX 126, SPECULATOR, NY 12164-0026
        [3] => Printed
        [4] => ="9405511899563327"
        [5] => 
        [6] => USPS
        [7] => Priority Mail (R)
        [8] => USPS Tracking
        [9] => 
        [10] => 
        [11] => 1lb 8oz
        [12] => 1/20/2016
        [13] => E-refund
    )

[3] => Array
    (
        [0] => 1/20/2016
        [1] => $8.92
        [2] => billy flyn, PO BOX 696, P.O. BOX 696, WESTCLIFFE, CO 81252-1696
        [3] => Printed
        [4] => ="94063388113621"
        [5] => 
        [6] => USPS
        [7] => Priority Mail (R)
        [8] => USPS Tracking
        [9] => 
        [10] => 
        [11] => 1lb 8oz
        [12] => 1/20/2016
        [13] => E-refund
    )

I want to be able to grab the last two comma-separated values in the second element, (i.e. the state and the city) and put them into their own element so it would look like this:

[2] => Array
    (
        [0] => 1/20/2016
        [1] => $8.15
        [2] => Kist Benings, PO BOX 126, 
        [3] => SPECULATOR 
        [4] => NY 12164-0026
        [5] => Printed
        [6] => ="9405511899563327"
        [8] => 
        [9] => USPS
        [10] => Priority Mail (R)
        [11] => USPS Tracking
        [12] => 
        [13] => 
        [14] => 1lb 8oz
        [15] => 1/20/2016
        [16] => E-refund
    )

My code:

<?php

function csv_to_array($filename='', $delimiter=',')
{
    if(!file_exists($filename) || !is_readable($filename))
        return FALSE;

    $header = NULL;
    $data = array();
    if (($handle = fopen($filename, 'r')) !== FALSE)
    {
        while (($row = fgetcsv($handle, 1000, $delimiter)) !== FALSE)
        {
            if(!$header)
                $header = $row;
            else
                $data[] = array_combine($header, $row);
        }
        fclose($handle);
    }
    return $data;
}

echo "<pre>";
print_r(csv_to_array('PrintHistory.csv'));
echo "</pre>";

die();
?>

Solution

  • Assuming the records are consistant:

    Take the Recipient and explode it. The output record is them built using array slices.

    clumsy, but it does the job...

    working demo...

    The Code:

    // output in here
    $out = array();
    
    // lose first record...
    array_shift($data);
    
    foreach ($data as $details) {
    
        $recipient = explode(',', $details[2]);
    
        $outRec = array_merge(
                    array_slice($details, 0, 2),
    
                     array($recipient[0] .', '. $recipient[1]),
                     array_slice($recipient, 2, 1),
                     array_slice($recipient, 3, 1),
    
                     array_slice($details, 3)
                );
    
         $out[] = $outRec;
    }