如何计算数组中的值?

How can I count the values in an array?

我试图打印数组的所有值,但我似乎无法获得数组的总数。我试过 count()、sizeOf() 和 array_count_values,但这些函数似乎都不起作用。

$query = DB::getInstance()->query("SELECT orderStatus FROM customerOrders");                


foreach ($query->results() as $orderered) {

   $result_array = array($orderered);
 //print_r($result_array);

  $orderData = array_map(function ($object) { return $object->orderStatus; }, $result_array);

  $test = json_decode(json_encode($result_array), true);



  $ORvalue = serialize($test);


  $ORvalue2 = unserialize($ORvalue);


  $orderValueNEW = call_user_func_array('array_merge', $ORvalue2);


  print_r($orderValueNEW);//debug 


 }//close foreach loop

数组打印后的结果:

Array ( [orderStatus] => 0 ) 
Array ( [orderStatus] => 0 ) 
Array ( [orderStatus] => 0 ) 
Array ( [orderStatus] => 1 ) 
Array ( [orderStatus] => 1 ) 

执行 count() 和 sizeOf 后:

11111 

运行array_count_values之后:

echo (array_count_values($orderValueNEW));
ArrayArrayArrayArrayArray 

在我看来你可能 运行 count($orderValueNew);每次确实包含一个数组,键为 orderstatus。 orderstatus 的值为 0 或 1 没有区别。

count 函数当然可以在这里工作:

$results = $query->results();

$num = count($values);

foreach($results as $ordered) { // etc.