if condition for huge amount of object properties
I have to show the record from database if at least one filed is filled with value out of 50 fields for a row.
I fetched data from database successfully. Stored in array of object like this
$obj[0]->prop1;
$obj[0]->prop2;
$obj[1]->prop1;
$obj[1]->prop2;
There are more than 50 properties for a object.
I have to check each property if one of them is not empty show that record.
I have a long if like this
if ($obj[$counter]->prop1 !='' || $开发者_如何学Goobj[0]->prop2 !='' ...
echo "show record"
wanted to know if there is shorten way. let me know if anything is not clear
Declare a function isEmpty()
in the class. For each instance, you will have to only call
if(!($obj[$counter]->isEmpty()) {
...
}
I guess it's better to alter your query:
select f1, f2, ... f50, f1||f2...||f50 as fsum from ...
and then you can check if $obj[51] != ''
this will perform faster than php
code...
No, you have to check each property individually...
So I found the easy way myself. You can just do a type cast the object to an array and check that array for values like this
foreach ((array)$data[$counter] as $value)
{
if($value !='')
{
$addRec=true;
}
}
精彩评论