所以我有这个代码,它接受一个文件并输出数组,就像我需要的那样但是,我希望能够计算st
和city
的次数,然后输出它像这样
// atanta, ga, 5
// new albany, ny, 10
// green bay, wi, 3
// ...
这是我的代码,它使文件成为一个数组:
<?php
$array = array_map('str_getcsv', file('PrintHistory.csv'));
$my_array = array();
$new_header = array("Print Date","Cost","Recipient","City","State and Zip","Status","Tracking #/Insurance ID","Date Delivered","Carrier","Class Service","Special Services","Insured Value","Cost Code","Weight","Mail Date","Refund Type");
$my_array[0] = $new_header;
foreach ($array as $key => $row) {
if( $key == 0 ) continue;
$my_row = array();
foreach ($row as $key2 => $value) {
// if this is the recipient column
if( $key2 == 2 ) {
// explode the value
$exp = explode(',', $value);
// we insert a value in the 4 next column ("Recipient","PO BOX/Address","City","State and Zip")
// we assume that the $exp[0] = recipient name; $exp[1] = po box/address; ...
$address = '';
$secondtolast = trim($exp[count($exp)-2]);
$last = trim(end($exp));
foreach($exp as $val){
if( $val == $secondtolast || $val == $last ) continue;
$address = $address . $val;
}
array_push($my_row, $address, $secondtolast, $last);
}
else array_push($my_row, $value);
}
array_push($my_array, $my_row);
}
echo "<pre>";
print_r($my_array);
echo "</pre>";
die();
以下是一些要查看的数组:
[1] => Array
(
[0] => 1/20/2016
[1] => $8.15
[2] => Jon Los Batos PO BOX 1232 BLANCO TX 78606-1232
[3] => BLANCO
[4] => TX 78606-1232
[5] => Printed
[6] => ="940551488859"
[7] =>
[8] => USPS
[9] => Priority Mail (R)
[10] => USPS Tracking
[11] =>
[12] =>
[13] => 1lb 8oz
[14] => 1/20/2016
[15] => E-refund
)
[2] => Array
(
[0] => 1/20/2016
[1] => $8.15
[2] => Kia Bennings PO BOX 2446 BLANCO TX 78606-1232
[3] => BLANCO
[4] => TX 78606-1232
[5] => Printed
[6] => ="9405511899563827"
[7] =>
[8] => USPS
[9] => Priority Mail (R)
[10] => USPS Tracking
[11] =>
[12] =>
[13] => 1lb 8oz
[14] => 1/20/2016
[15] => E-refund
)
[3] => Array
(
[0] => 1/20/2016
[1] => $8.92
[2] => Bavis lynn PO BOX 196 P.O. BOX 196 WESTCLIFFE CO 81252-1696
[3] => WESTCLIFFE
[4] => CO 81252-1696
[5] => Printed
[6] => ="9405563388113621"
[7] =>
[8] => USPS
[9] => Priority Mail (R)
[10] => USPS Tracking
[11] =>
[12] =>
[13] => 1lb 8oz
[14] => 1/20/2016
[15] => E-refund
)
[4] => Array
(
[0] => 1/20/2016
[1] => $10.77
[2] => Ally Hudson PSC 4845 BOX 3734 FPO AP 96321-0004
[3] => FPO
[4] => AP 96321-0004
[5] => Printed
[6] => ="9405511899563388396642"
[7] =>
[8] => USPS
[9] => Priority Mail (R)
[10] => USPS Tracking
[11] =>
[12] =>
[13] => 1lb 8oz
[14] => 1/20/2016
[15] => E-refund
)
[5] => Array
(
[0] => 1/20/2016
[1] => $6.60
[2] => Bark Heinteman PO BOX 2369 CHARLOTTE COURT HOUSE VA 23923-0269
[3] => CHARLOTTE COURT HOUSE
[4] => VA 23923-0269
[5] => Printed
[6] => ="940551189950145"
[7] =>
[8] => USPS
[9] => Priority Mail (R)
[10] => USPS Tracking
[11] =>
[12] =>
[13] => 1lb 8oz
[14] => 1/20/2016
[15] => E-refund
)
答案 0 :(得分:0)
您可以在构建 $ my_array 后立即添加此代码:
// count cities
$summary = array();
foreach($my_array as $element) {
$key = $element[3] . ", " . explode(" ", $element[4])[0];
$summary[$key] = isset($summary[$key]) ? $summary[$key] + 1 : 1;
}
// print summary
foreach($summary as $city => $count) {
echo "$city, $count<br>";
}
使用您提供的 $ my_array 内容,您将获得此输出:
BLANCO,TX,2
WESTCLIFFE,CA,1
FPO,AP,1
CHARLOTTE COURT HOUSE,VA,1
答案 1 :(得分:0)
你可以试试吗?
$array = array_map('str_getcsv', file('PrintHistory.csv'));
$my_array = array();
$new_header = array("Print Date", "Cost", "Recipient", "City", "State and Zip", "Status", "Tracking #/Insurance ID", "Date Delivered", "Carrier", "Class Service", "Special Services", "Insured Value", "Cost Code", "Weight", "Mail Date", "Refund Type");
$my_array[0] = $new_header;
$cities = array(); // cities to count.
foreach ($array as $key => $row) {
if ($key == 0) {
continue;
}
$my_row = array();
foreach ($row as $key2 => $value) {
// if this is the recipient column
if ($key2 == 2) {
// explode the value
$exp = explode(',', $value);
// we insert a value in the 4 next column ("Recipient","PO BOX/Address","City","State and Zip")
// we assume that the $exp[0] = recipient name; $exp[1] = po box/address; ...
$address = '';
$secondtolast = trim($exp[count($exp) - 2]);
$last = trim(end($exp));
foreach ($exp as $val) {
if ($val == $secondtolast || $val == $last) {
continue;
}
$address = $address . $val;
}
array_push($my_row, $address, $secondtolast, $last);
} else {
array_push($my_row, $value);
}
$cities[] = $my_row[3] . ', ' . $my_row[4]; // adding cities to it
}
array_push($my_array, $my_row);
}
// count and calculate multiple occurences
$cityCounts = array_count_values($cities));
$citiesMultipleAppears = array();
foreach ($cityCounts as $k => $v) {
if ($v > 1) {
$citiesMultipleAppears[] = $k . ', ' . $v;
}
}
echo "<pre>";
print_r($my_array);
print_r($citiesMultipleAppears);
echo "</pre>";