简体   繁体   中英

Scroll through muntidimensional array

I have this array:

Array ( [0] => Array ( [Moeda] => AUDCAD [Data1] => 2020-11-27 10:20 [Hora] => 10:20:00 [Vela] => Verde [Timeframe] => 1 )
 [1] => Array ( [Moeda] => AUDCAD [Data1] => 2020-11-26 10:20 [Hora] => 10:20:00 [Vela] => Vermelha [Timeframe] => 1 )
 [2] => Array ( [Moeda] => AUDCAD [Data1] => 2020-11-25 10:20 [Hora] => 10:20:00 [Vela] => Verde [Timeframe] => 1 ) )

and I'm trying to go through his fields like this:

for($i = 0; $i < count($resultado); $i++) {          
        foreach($resultados[$i] as $key => $value) {
               $horario = $value->Hora;
               print_r('Horario: '. $horario); 
                
                if (!in_array($horario, $analise))
                {
                $arr = array($horario => array('Verde'=> 0, 'Vermelha'=> 0, 'Doji'=> 0, '%'=> 0, 'dir'=> ''));
                   $analise = array_merge($analise, $arr);
                    print_r( "concat\n");
                    print_r($analise);
                }
                
                $analise[$horario][$value['Vela']] += 1;
                
                try{

                    $analise[$horario]['%'] = round(100 * ($analise[$horario]['Verde'] / ($analise[$horario]['Verde'] + $analise[$horario]['Vermelha'] + $analise[$horario]['Doji'])));
                    echo "passou do try\n";
                } catch (Exception $e) {
                    // Do nothing
                }
        }
         
    }

But it doesn't work

maybe use 2 foreach, like

foreach($resultado as $i1 => $v1){
  foreach($v1 as $i2=>$v2){
    .....
  }
}

drop your line

for($i = 0; $i < count($resultado); $i++) {

just do the foreach

foreach($resultados AS $key => $value){

Call values this this

$horario = $value['Hora'];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM