计数数组键的值 [英] Counting the array keys values

查看:134
本文介绍了计数数组键的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有这个code,需要一个文件,就像我需要它的阵列输出,但是,我想应该能算多少次 ST 城市,然后输出它像这样

  // atanta,GA,5
//新奥尔巴尼,10
//格林贝,威斯康星,3
// ...

下面是code我有,使文件的数组:

 < PHP
  $阵列= array_map('str_getcsv',文件(PrintHistory.csv'));
  $ my_array =阵列();
  $ new_header =阵列(打印日期,成本,收件人,城市,国家和Zip,状态,跟踪#/保险ID,交付日期,开利,一流的服务,特殊服务,保险价值,成本code,重量,邮件日期,退款类型);  $ my_array [0] = $ new_header;  的foreach($数组$关键=> $行){
     如果($键== 0)继续;  $ my_row =阵列();
  的foreach($行为$键2 => $值){
      //如果这是接收者列
      如果($ KEY2 == 2){
         //爆炸的价值
         $ EXP =爆炸('',$值);
         //我们插入了4下一列的值(收件人,PO BOX /地址,城市,国家和Zip)
         //我们假设$ EXP [0] =收件人姓名; $ EXP [1] =邮政信箱/地址; ...
    $地址='';
    $ secondtolast =修剪($ EXP [计数($ EXP)-2]);
    $最后=修剪(完($ EXP));    的foreach($ EXP为$ VAL){
        如果($ VAL == $ secondtolast || $ VAL == $最后一)继续;
          $地址= $地址。 $ VAL;
    }    array_push($ my_row,$地址,$ secondtolast,$即止);  }
  其他array_push($ my_row,$值);
  }  array_push($ my_array,$ my_row);
  }
  回声< pre>中;
  的print_r($ my_array);
  回声< / pre>中;  死();

和这里的一些阵列来看看:

  [1] =>排列
    (
        [0] => 2016年1月20日
        [1] => $ 8.15
        [2] =>乔恩·洛Batos PO BOX 1232 BLANCO TX 78606-1232
        [3] => BLANCO
        [4] => TX 78606-1232
        [5] =>印刷的
        [6] => =940551488859
        [7] =>
        [8] => USPS
        [9] =>优先邮件(R)
        [10] => USPS跟踪
        [11] =>
        [12] =>
        [13] =>一磅8盎司
        [14] => 2016年1月20日
        [15] => E-退款
    )[2] =>排列
    (
        [0] => 2016年1月20日
        [1] => $ 8.15
        [2] =>起亚Bennings PO BOX 2446 BLANCO TX 78606-1232
        [3] => BLANCO
        [4] => TX 78606-1232
        [5] =>印刷的
        [6] => =9405511899563827
        [7] =>
        [8] => USPS
        [9] =>优先邮件(R)
        [10] => USPS跟踪
        [11] =>
        [12] =>
        [13] =>一磅8盎司
        [14] => 2016年1月20日
        [15] => E-退款
    )[3] =>排列
    (
        [0] => 2016年1月20日
        [1] => $ 8.92
        [2] => Bavis林恩PO BOX 196邮政信箱BOX 196韦斯特克利夫CO 81252-1696
        [3] =>韦斯特克利夫
        [4] => CO 81252-1696
        [5] =>印刷的
        [6] => =9405563388113621
        [7] =>
        [8] => USPS
        [9] =>优先邮件(R)
        [10] => USPS跟踪
        [11] =>
        [12] =>
        [13] =>一磅8盎司
        [14] => 2016年1月20日
        [15] => E-退款
    )[4] =>排列
    (
        [0] => 2016年1月20日
        [1] => $ 10.77
        [2] =>艾丽哈德森PSC 4845 BOX 3734 FPO AP 96321-0004
        [3] => FPO
        [4] => AP 96321-0004
        [5] =>印刷的
        [6] => =9405511899563388396642
        [7] =>
        [8] => USPS
        [9] =>优先邮件(R)
        [10] => USPS跟踪
        [11] =>
        [12] =>
        [13] =>一磅8盎司
        [14] => 2016年1月20日
        [15] => E-退款
    )[5] =>排列
    (
        [0] => 2016年1月20日
        [1] => $ 6.60
        [2] =>树皮Heinteman PO BOX 2369 CHARLOTTE COURT H​​OUSE VA 23923-0269
        [3] => CHARLOTTE COURT H​​OUSE
        [4] => VA 23923-0269
        [5] =>印刷的
        [6] => =940551189950145
        [7] =>
        [8] => USPS
        [9] =>优先邮件(R)
        [10] => USPS跟踪
        [11] =>
        [12] =>
        [13] =>一磅8盎司
        [14] => 2016年1月20日
        [15] => E-退款
    )


解决方案

您已经建立后,您可以添加此code正确的 $ my_array 的:

  //算城市
$总结=阵列();
的foreach($ my_array为$元素){
    $关键= $元素[3]。 ,。爆炸(,$元件[4])[0];
    $总结[$关键] =使用isset($汇总[$关键])? $概要[$键〕+ 1:1;
}//打印摘要
的foreach($总结为$城市= GT; $计数){
    回声$城市$ COUNT< BR>中;
}

随着 $ my_array 的你提供的内容,你会得到这样的输出:


  

BLANCO,TX,2结果
  韦斯特克利夫,CA 1搜索
  FPO,AP,1结果
  CHARLOTTE COURT H​​OUSE,VA,1


So I have this code that takes a file and outputs the array like I need it to but, I would like to be able to count how many times st and city, and then output it like this

// atanta, ga, 5
// new albany, ny, 10  
// green bay, wi, 3 
// ...

Here is the code I have that that makes the file an array:

  <?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();

And here is some of the array to look at:

[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
    )

解决方案

You could add this code right after you have built $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>";
}

With the $my_array content you provided, you would get this output:

BLANCO, TX, 2
WESTCLIFFE, CA, 1
FPO, AP, 1
CHARLOTTE COURT HOUSE, VA, 1

这篇关于计数数组键的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆