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

215
Views
Obtenga el estado de una matriz multidimensional con el mismo nombre de clave en php

Tengo una matriz como esta ..

 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] => ) )

De esta matriz, necesito tomar un estado usando el profit_percentage de beneficio si alguno de los profit_percentage de beneficio tiene un valor, lo que significa que necesito obtener un estado como si existiera un beneficio para al menos un usuario .

Supongamos que si mi matriz es como la siguiente

 Array ( [0] => stdClass Object ( [id] => 29 [name] => User1 [activated] => 1 [profit_percentage] => ) [1] => stdClass Object ( [id] => 23 [name] => User2 [activated] => 1 [profit_percentage] => ) )

Entonces el estado debe ser como si la ganancia no existiera.

¿Cómo debo obtener esto?

over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

Haga un bucle en el objeto de matriz y verifique profit_percentage is not empty como este

 <?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

Supongamos que si sus datos están en la variable $data_array , haga algo como lo siguiente

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

Y su matriz es stdClass Object, por eso $data->profit_percentage no $data['profit_percentage'] .

over 4 years ago · Santiago Trujillo Report

0

Puede usar un bucle foreach simple y 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!