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 我可以把它从那里。谢谢。
请修改输出到一个更好的格式。如果不可能读取数据的结构,那真的很难提供帮助。 –