Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

210
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda