PHP计算多维数组中的数组键

时间:2019-07-10 19:58:43

标签: php arrays count

我有一个像这样的简单数组:

!= null

然后我只想计算数组键[Colors]和[Sizes],它们总共应该给我2个,但是使用count($ array)之类的count()会抛出var query = db.SALETABLES .SelectMany(ta => db.SALES .Where(s => ta.ID == s.ID_TABLE && s.CLOSEDTIME == null).Take(1), // <-- (ta, s) => new { ta.ID, ta.DESCRIPTION, NR_SALE = s != null ? s.NR_SALE : 0, IDSALE = s != null ? s.ID : 0, IDUSER = s != null ? s.IDUSER : 0, USERNAME = s != null ? s.USERS.USERNAME : "", SALESUM = s != null ? s.SALES_DETAIL.Sum(p => p.PRICE * p.CANT) : 0 });

2 个答案:

答案 0 :(得分:0)

您有一个具有两个属性的对象(ColorsSizes)。 (http://localhost:4200/a_route_that_not_exists

使用get_object_varshttps://www.php.net/manual/en/language.types.object.php)来获取对象的属性,然后对其进行计数:

$number_of_properties = count(get_object_vars($your_std_class_object));

答案 1 :(得分:0)

假设您的变量为$obj

count(get_object_vars($obj));
// return 2, for your object variables are two: Colors & Sizes (both arrays)


count($obj->Colors);
// return 1, for your object variable (Colors) (which is array) has only one element


count($obj->Sizes);
// return 2, for your object variable (Sizes) (which is array) has two elements


count(get_object_vars($obj->Sizes[0]));
// return 1, for your object variable(Size)'s [0] index has only 1 object element, i.e. 10