0

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

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

这是仅来自两个订单的订单数据。我需要计算每个州、每个城市、shipmethod 等在数组中出现的次数。我尝试了以下方法,但它只计算了 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,我可以从那里得到它。谢谢你。

4

1 回答 1

0

从您显示的输出中,您正在处理一个对象数组,而不是关联数组,因此您不能像处理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
于 2012-12-14T05:01:02.577 回答