0
我使用kohana,當您嘗試從數據庫中獲取數據時,它會返回類變量(如$ user-> firstname)作爲數據庫數據。用戶表有12列,我拿8列,但在這一點colomuns可能是空的(如$用戶 - >手機)。 ?我怎樣才能找到空列號(正確的方式..)用於計數空變量的正確方法
非常感謝
我使用kohana,當您嘗試從數據庫中獲取數據時,它會返回類變量(如$ user-> firstname)作爲數據庫數據。用戶表有12列,我拿8列,但在這一點colomuns可能是空的(如$用戶 - >手機)。 ?我怎樣才能找到空列號(正確的方式..)用於計數空變量的正確方法
非常感謝
一般地,你可以嘗試這樣的:
/**
* Count number of empty data members in a row object.
*/
function countEmpty($row){
$fields = array_keys($row->as_array());
$cnt = 0;
foreach($fields as $f){
if (empty($row->$f)) $cnt++;
}
return $cnt;
}
我找到解決方案。 PHP有神奇的get_object_vars功能:
$data = User_Model::factory()->read(
array('id' => $user_id),
'firstname, lastname, birthday, country, mobilephone, landphone, address'
);
$filled_data = 0;
foreach(get_object_vars($data) as $v)
{
if ($v != '') $filled_data++;
}
return round($filled_data/count(get_object_vars($data)) * 100);
你可以使用count(array_filter($ data-> as_array())); – gpilotino