所以这可能是一个简单的问题,但我想不通,我将如何获取数组数组中的数据?如果有意义的话,我想从数组的第二个元素中获取最后两个逗号分隔的值。
我的数组如下所示:
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
)
我希望能够在第二个元素(即州和城市)中获取最后两个逗号分隔的值,并将它们放入自己的元素中,所以它看起来像这样:
[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
)
我的代码:
<?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();
?>