Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

209
Views
Get status from multidimensional array with same key name in php

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?

over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

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;

    ?>
over 4 years ago · Santiago Trujillo Report

0

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'].

over 4 years ago · Santiago Trujillo Report

0

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";


?>
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!