2012-12-14 41 views
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] => [email protected] [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] => [email protected] [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 我可以把它從那裏。謝謝。

+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