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

これは、2つの注文のみの注文データです。各州、各都市、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 に答える