繁体   English   中英

计算多维数组中的特定值

[英]count specific values in a multidimensional array

我有一组奇怪的数组,需要计算特定值在结果中显示多少次。 目前,我有这段代码。

$nested_arrays = shopp_orders( '2011-11-30 00:00:00', '2012-11-30 12:59:59', false, '', 2 );
print_r($nested_arrays);

此代码从数据库中提取多个数组(序列化数据),并像这样输出

Array ( [30] => Purchase Object (
        [purchased] => Array ( )
        [columns] => Array ( )
        [message] => Array ( )
        [data] => Array ( )
        [invoiced] => [authorized] => [captured] => [refunded] => [voided] => [balance] => 0 [downloads] => [shipable] => [shipped] => [stocked] => [_position:DatabaseObject:private] => 0 [_properties:DatabaseObject:private] => Array ( )
        [_ignores:DatabaseObject:private] => Array ( [0] => _ )
        [_map:protected] => Array ( )
        [_table] => wp_shopp_demo_shopp_purchase [_key] => id [_datatypes] => Array ( 
        [id] => int [customer] => int [shipping] => int [billing] => int [currency] => int [ip] => string [firstname] => string [lastname] => string [email] => string [phone] => string [company] => string [card] => string [cardtype] => string [cardexpires] => date [cardholder] => string [address] => string [xaddress] => string [city] => string [state] => string [country] => string [postcode] => string [shipname] => string [shipaddress] => string [shipxaddress] => string [shipcity] => string [shipstate] => string [shipcountry] => string [shippostcode] => string [geocode] => string [promos] => string [subtotal] => float [freight] => float [tax] => float [total] => float [discount] => float [fees] => float [taxing] => list [txnid] => string [txnstatus] => string [gateway] => string [paymethod] => string [shipmethod] => string [shipoption] => string [status] => int [data] => string [secured] => string [created] => date [modified] => date ) [_lists] => Array ( [taxing] => Array ( [0] => exclusive [1] => inclusive ) )
        [id] => 30 [customer] => 12 [shipping] => 23 [billing] => 23 [currency] => 0 [ip] => 24.125.58.205 [firstname] => test [lastname] => test [email] => webmaster@note2voter.com [phone] => 1234567890 [company] => [card] => 1111 [cardtype] => Visa [cardexpires] => 1420070400 [cardholder] => test [address] => 123 Any Street [xaddress] => [city] => Danville [state] => VA [country] => US [postcode] => 24541 [shipname] => [shipaddress] => 123 Any Street [shipxaddress] => [shipcity] => Danville [shipstate] => VA [shipcountry] => US [shippostcode] => 24541 [geocode] => [promos] => Array ( ) [subtotal] => 49.37 [freight] => 9.98 [tax] => 9.874 [total] => 69.22 [discount] => 0 [fees] => 0 [taxing] => exclusive [txnid] => [txnstatus] => authed [gateway] => TestMode [paymethod] => credit-card-test-mode [shipmethod] => ItemRates-0 [shipoption] => Fast Shipping [status] => 0 [secured] => [created] => 1354096946 [modified] => 1354096946
        )

    [29] => Purchase Object (
        [purchased] => Array ( )
        [columns] => Array ( )
        [message] => Array ( )
        [data] => Array ( )
        [invoiced] => [authorized] => [captured] => [refunded] => [voided] => [balance] => 0 [downloads] => [shipable] => [shipped] => [stocked] => [_position:DatabaseObject:private] => 0 [_properties:DatabaseObject:private] => Array ( )
        [_ignores:DatabaseObject:private] => Array ( [0] => _ ) [_map:protected] => Array ( ) [_table] => wp_shopp_demo_shopp_purchase [_key] => id [_datatypes] => Array ( [id] => int [customer] => int [shipping] => int [billing] => int [currency] => int [ip] => string [firstname] => string [lastname] => string [email] => string [phone] => string [company] => string [card] => string [cardtype] => string [cardexpires] => date [cardholder] => string [address] => string [xaddress] => string [city] => string [state] => string [country] => string [postcode] => string [shipname] => string [shipaddress] => string [shipxaddress] => string [shipcity] => string [shipstate] => string [shipcountry] => string [shippostcode] => string [geocode] => string [promos] => string [subtotal] => float [freight] => float [tax] => float [total] => float [discount] => float [fees] => float [taxing] => list [txnid] => string [txnstatus] => string [gateway] => string [paymethod] => string [shipmethod] => string [shipoption] => string [status] => int [data] => string [secured] => string [created] => date [modified] => date ) [_lists] => Array ( [taxing] => Array ( [0] => exclusive [1] => inclusive ) )
           [id] => 29 [customer] => 13 [shipping] => 26 [billing] => 25 [currency] => 0 [ip] => 70.176.223.40 [firstname] => Bryan [lastname] => Crawford [email] => bjcisfree@gmail.com [phone] => 4802323049 [company] => ggg [card] => 1111 [cardtype] => Visa [cardexpires] => 1356998400 [cardholder] => ggg [address] => 1300 W Warner Rd [xaddress] => [city] => Gilbert [state] => AZ [country] => US [postcode] => 85224 [shipname] => [shipaddress] => 1300 W Warner Rd [shipxaddress] => [shipcity] => Gilbert [shipstate] => AZ [shipcountry] => US [shippostcode] => 85224 [geocode] => [promos] => Array ( ) [subtotal] => 29.95 [freight] => 9.98 [tax] => 0 [total] => 39.93 [discount] => 0 [fees] => 0 [taxing] => exclusive [txnid] => [txnstatus] => authed [gateway] => TestMode [paymethod] => credit-card-test-mode [shipmethod] => ItemRates-0 [shipoption] => Fast Shipping [status] => 0 [secured] => [created] => 1353538691 [modified] => 1353538691 
        )
   ) 

这是仅来自两个订单的订单数据。 我需要计算数组中每个州,每个城市,造船方法等发生的次数。 我尝试了以下操作,但它只计算了2个大数组。

function count_nested_array_keys(array &$a, array &$res=array()) {
    $i = 0;
    foreach ($a as $key=>$value) {
        if (is_array($value)) {
             $i += count_nested_array_keys($value, &$res);
        }
        else {
             if(!isset($res[$key])) $res[$key] = 0;

             $res[$key]++;
             $i++;
        }
    }
    return $i;
}

$total_item_count = count_nested_array_keys($nested_arrays, $count_per_key);

echo "count per key: ", print_r($count_per_key), "\n";

如果有人可以告诉我如何计算每个状态值出现了多少次,例如VA = 2 NC = 1,那么我可以从那里获取它。 谢谢。

从输出中可以看到,您正在处理的是对象数组,而不是关联数组,因此您无法像使用数组一样使用foreach语句访问对象属性中的值。

但是,您可以使用get_object_vars( object $object) PHP函数获得对象属性的关联数组。

这是一些示例代码:

<?php

class Purchase {
    var $state;
    var $country;

    public function __construct($state, $country) {
        $this->state = $state;
        $this->country = $country;
    }
}

$arr = Array(
    new Purchase('VA', 'USA'), 
    new Purchase('VA', 'USA'), 
    new Purchase('NY', 'USA')
);

var_dump($arr);

$counts = array();

foreach($arr as $p) {
    foreach(get_object_vars($p) as $key => $value) {
        if(isset($counts[$value]))
            $counts[$value]++;
        else
            $counts[$value] = 1;
    }
}

var_dump($counts);

输出:

array
  0 => 
    object(Purchase)[1]
      public 'state' => string 'VA' (length=2)
      public 'country' => string 'USA' (length=3)
  1 => 
    object(Purchase)[2]
      public 'state' => string 'VA' (length=2)
      public 'country' => string 'USA' (length=3)
  2 => 
    object(Purchase)[3]
      public 'state' => string 'NY' (length=2)
      public 'country' => string 'USA' (length=3)
array
  'VA' => int 2
  'USA' => int 3
  'NY' => int 1

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM