php数组以动态结果计入 [英] php array count in with dynamic result

查看:74
本文介绍了php数组以动态结果计入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试计算从脚本返回的数组结果.有两种情况我可以取回选项a是

i am trying to count a result of a array i get back from a script. there are two cases that i can get back option a is

Array ( [Id] => 1779 [SupplierId] => 1809 [SupplierName] => cccccc)

第二种选择ib

Array ( 
[0] => Array ( [Id] => 2020 [SupplierId] => 1809 [SupplierName] => vvv) 
[1] => Array ( [Id] => 2058 [SupplierId] => 1809 [SupplierName] => bbb) 
[2] => Array ( [Id] => 2063 [SupplierId] => 1809 [SupplierName] => xx) 
)

如果我确实考虑到我回来的情况3在选项A中我如何算回1,而在选项B中我会算3?

if i do count in to cases i get back 3 How can i count that in option A i get back 1, and in option B i will get back 3 ?

推荐答案

您可以创建如下函数:

function countRes($arr) {
    return is_array(end($arr)) ? count($arr) : 1;
}

$arr1 = array("Id" => 1779, "SupplierId" => 1809, "SupplierName" => "cccccc");

$arr2 = array(array("Id" => 2020, "SupplierId" => 1809, "SupplierName" => "vvv"),
              array("Id" => 2058, "SupplierId" => 1809, "SupplierName" => "bbb"),
              array("Id" => 2063, "SupplierId" => 1809, "SupplierName" => "xx"));

echo countRes($arr1); // 1
echo countRes($arr2); // 3

它检查最后一个元素是否为数组,并返回结果数组中的数组数.否则返回1,因为结果数组本身包含数据.

It checks if the last element is an array, and returns the number of arrays in the resulting array. Otherwise it returns 1 because the resulting array itself contains the data.

这篇关于php数组以动态结果计入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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