json 对象中的计数值需要数组PHP [已关闭]

mm9b1k5b  于 2022-12-24  发布在  PHP
关注(0)|答案(5)|浏览(149)

已关闭。此问题需要details or clarity。当前不接受答案。
**想要改进此问题?**添加详细信息并通过editing this post阐明问题。

昨天关门了。
Improve this question
我有一个如下所示的数组,有没有办法计算CID出现的总次数。例如:

身份识别码= 992010000021102

我想把结果算作2

- Laravel 6,php 7.4

{
    "status": true,
    "message": null,
    "data": [
        [
            {
                "cid": "99201000021102",
                "mailaddress": "99201000021102@gmail.com"
            },
            {
                "cid": "99201000021105",
                "mailaddress": "99201000021105@gmail.com"
            }
        ],
        [
            {
                "cid": "00003900062153",
                "mailaddress": "00003900062153@designone.jp"
            }
        ],
        [
            {
                "cid": "99201000021102",
                "mailaddress": "99201000021102@gmail.com"
            }
        ],
        [
            {
                "cid": "99201000021101",
                "mailaddress": "99201000021101@gmail.com"
            }
        ]
    ]
}

我尝试运行循环,但结果与预期不符。

a11xaf1n

a11xaf1n1#

你可以试试这个:

$countArr = [];
foreach ($arr as $val){
    foreach ($val as $value){
        if (!array_key_exists($value['cid'], $countArr)){
            $countArr[$value['cid']] = 1;
        }else $countArr[$value['cid']] += 1;
    }
}
print_r($countArr);

它将打印:

Array
(
    [99201000021102] => 2
    [99201000021105] => 1
    [00003900062153] => 1
    [99201000021101] => 1
)

因为主数组中的每个数组都有一些对象,所以我们需要嵌套循环,每次检查CID是否存在于最终数组中,如果存在,我们将其与1相加,否则我们将推送新的CID

k3bvogb1

k3bvogb12#

若要计算特定值在数组中出现的总次数,可以使用循环迭代数组,并在每次找到该值时递增计数器变量。例如,在您的示例中..

$array = [
    [
        [
            "cid" => "99201000021102",
            "mailaddress" => "hainghia.developer@gmail.com"
        ],
        [
            "cid" => "99201000021105",
            "mailaddress" => "thiendi@gmail.com"
        ]
    ],
    [
        [
            "cid" => "00003900062153",
            "mailaddress" => "sawaya.keiichiro@designone.jp"
        ]
    ],
    [
        [
            "cid" => "99201000021102",
            "mailaddress" => "hainghia.developer@gmail.com"
        ]
    ],
    [
        [
            "cid" => "99201000021101",
            "mailaddress" => "kankocho_auth@gmail.com"
        ]
    ]
];

$cid = "99201000021102";
$count = 0;

foreach ($array as $item) {
    foreach ($item as $innerItem) {
        if ($innerItem['cid'] == $cid) {
            $count++;
        }
    }
}

echo $count; // Outputs 2
epfja78i

epfja78i3#

你也可以在jQuery中得到你的结果,试试这个.

var json = {
    "status": true,
    "message": null,
    "data": [
        [
            {
                "cid": "99201000021102",
                "mailaddress": "99201000021102@gmail.com"
            },
            {
                "cid": "99201000021105",
                "mailaddress": "99201000021105@gmail.com"
            }
        ],
        [
            {
                "cid": "00003900062153",
                "mailaddress": "00003900062153@designone.jp"
            }
        ],
        [
            {
                "cid": "99201000021102",
                "mailaddress": "99201000021102@gmail.com"
            }
        ],
        [
            {
                "cid": "99201000021101",
                "mailaddress": "99201000021101@gmail.com"
            }
        ]
    ]
};
var testOp = getKeys(json,"99201000021102");
alert('Serach result found no of time : '+testOp);
function getKeys(obj, val) {
    var objects = [];
    for (var i in obj) {
        if (!obj.hasOwnProperty(i)) continue;
        if (typeof obj[i] == 'object') {
            objects = objects.concat(getKeys(obj[i], val));
        } else if (obj[i] == val) {
            objects.push(i);
        }
    }
    return objects.length;
}
ct2axkht

ct2axkht4#

  • 您可以在数据数组上使用Laravel的collection helpers,非常简单
  • 将数据数组转换为集合,并将1个级别和groupBy“cid”列扁平化

$收集结果=收集($数据)-〉展平(1)-〉分组依据(“cid”);

*$data变量是从您的响应中获取的数据数组,并且需要根据您的数据集进行扁平化。我们只需要应用一个级别的扁平化,就可以将数据作为流正确分组。

  • 打印输出结果,你会得到你预期的结果。
kmpatx3s

kmpatx3s5#

使用array_filter的简单解决方案:

$count = count(
    array_filter(
        $yourarray->data,
        function($v) {
            return $v[0]->cid === '99201000021102';
        }
    )
);

说明:

  • $v[0]->cid与我们搜索的值进行比较
  • 我们有一个function,它作为第二个参数传递给array_filterarray_filter将确定哪些项将在结果中结束,而$yourarray->data作为第一个参数传递
  • array_filter的结果是一个数组,其中包含functiontrue的项,而不包含其他项
  • 使用count,我们得到过滤数组中的元素数

此外,如果您事先不知道要搜索的值,请使用use规范,如

$a = '99201000021102';
$count = count(
    array_filter(
        $yourarray->data,
        function($v) use ($a) {
            return $v[0]->cid === $a;
        }
    )
);

相关问题