PHP多维数组数 [英] PHP Multidimensional Array Count

查看:123
本文介绍了PHP多维数组数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下的数组,我要算PaymentType==贝宝的元素。我用这个方法
$ TOTALCOUNT = 计数(array_filter((阵列)$ DATA [0],'贝宝'));
但得到警告贝宝,应该是一个有效的回调
鸭preciate任何回应,以帮助我得到正确的方向前进。

 阵列

[0] =>排列
    (
        [TransactionDate] => 0000-00-00 00:00:00
        [总金额] => 0.00
        [PayPalFee] => 2.48
        [PayPalTransactionID] => 92
        [PaymentType] =>贝宝
    )[1] =>排列
    (
        [TransactionDate] => 0000-00-00 00:00:00
        [总金额] => 0.00
        [PayPalFee] => 2.48
        [PayPalTransactionID] => 3
        [PaymentType] =>贝宝
    )[2] =>排列
    (
        [TransactionDate] => 2011-05-16 11点15分02秒
        [总金额] => 75.00
        [PayPalFee] => 2.48
        [PayPalTransactionID] => 2
        [PaymentType] =>贝宝
    )


解决方案

array_filter()应该返回滤波阵列(也需要一个回调函数来工作,不是搜索值) - 你不需要这么做。这里是真正只计算匹配的解决方案:

  $ TOTALCOUNT = 0;
为($ i = 0,$ C =计数($数据[0]); $ I< $ C,$ I ++)
{
    如果($数据[0] [$ i] ['PaymentType'] ===贝宝)
        $ TOTALCOUNT ++;
}

I have the following array that I want to count the element of "PaymentType" == paypal. I am using this method $totalcount = count(array_filter((array) $data[0], 'paypal')); but get the warning "'paypal', should be a valid callback" Appreciate any response to help me get going in the right direction.

Array
(
[0] => Array
    (
        [TransactionDate] => 0000-00-00 00:00:00
        [TotalAmount] => 0.00
        [PayPalFee] => 2.48
        [PayPalTransactionID] => 92
        [PaymentType] => paypal
    )

[1] => Array
    (
        [TransactionDate] => 0000-00-00 00:00:00
        [TotalAmount] => 0.00
        [PayPalFee] => 2.48
        [PayPalTransactionID] => 3
        [PaymentType] => paypal
    )

[2] => Array
    (
        [TransactionDate] => 2011-05-16 11:15:02
        [TotalAmount] => 75.00
        [PayPalFee] => 2.48
        [PayPalTransactionID] => 2
        [PaymentType] => paypal
    )
)

解决方案

array_filter() is supposed to return a filtered array (and also needs a callback function to work, not a search value) - you don't need to do that. Here's a solution that really only counts the matches:

$totalcount = 0;
for ($i = 0, $c = count($data[0]); $i < $c; $i++)
{
    if ($data[0][$i]['PaymentType'] === 'paypal')
        $totalcount++;
}

这篇关于PHP多维数组数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆