I have an array like this..
Array
(
[0] => stdClass Object
(
[id] => 29
[name] => User1
[activated] => 1
[profit_percentage] => 34.0000
)
[1] => stdClass Object
(
[id] => 23
[name] => User2
[activated] => 1
[profit_percentage] =>
)
)
From this array I need to take a status by using the profit_percentage if any one of the profit_percentage has value mean I need to get status like profit is exists for at least one user..
Suppose If my array is like below
Array
(
[0] => stdClass Object
(
[id] => 29
[name] => User1
[activated] => 1
[profit_percentage] =>
)
[1] => stdClass Object
(
[id] => 23
[name] => User2
[activated] => 1
[profit_percentage] =>
)
)
Then the status must be like profit does not exists
How should I get this?
Loop the array object and check profit_percentage is not emptylike this
<?php
$status =0;
foreach($loop as $row )
{
if($row->profit_percentage !="" && $row->profit_percentage>0)
{
$status =1;
break;
}
}
echo "status is ".$status;
?>
Suppose if your data in the variable $data_array then do sothing like following
$count = 0;
foreach($data_array as $data){
if($data->profit_percentage!=""&&$data->profit_percentage>0){
$count = $count+1;
}
}
if($count>0){
echo "profit is exists for atleast one user";
}
else{
echo "profit does not exists";
}
And your array is stdClass Object thats why $data->profit_percentage not $data['profit_percentage'].
You can use simple foreach loop ans isset.
<?php
class Data {
var $name;
var $percentage;
function set_name($name) {
$this->name = $name;
}
function set_percentage($p) {
$this->percentage = $p;
}
}
$a = new Data();
$a->set_name("ASD");
//$a->set_percentage(56);
$b = new Data();
$b->set_name("erw");
$b->set_percentage(56);
$p = array($a, $b);
echo '<pre>';
print_r($p);
$status = false;
foreach ($p as $item) {
if (isset($item-> percentage)) {
$status = true;
break;
}
}
echo $status? "profit exists":"profit does not exists";
?>