Search array data for calculations

Asked

Viewed 21 times

-1

Save Galera, I’m looking for data that is in a multidimensional array and store the data in each variable to simulate spending and flights.

I cannot take data from the array and add it to the variable.

<?php 

$ar = 'A220-100';
$distancia = 3000;
$viagens = 3;

$avioes = [
    'A220-100' => array('Aeronave'=>'A220-100', 'Fabricante'=>'Airbus', 'Ano'=> 2016, 'Assentos'=> 133, 'Carga'=> 13.3, 'Velocidade'=>850, 'Alcance'=>4075, 'Consumo'=>2.85, 'Desgaste'=>1.3, 'Preco'=>52.500000),
    '737-500'=> array('Aeronave'=>'737-500', 'Fabricante'=>'Boeing', 'Ano'=> 1990, 'Assentos'=> 132, 'Carga'=> 15.2, 'Velocidade'=>786, 'Alcance'=>4725, 'Consumo'=>3.23, 'Desgaste'=>2.3, 'Preco'=>77.500000)
];

function Calcular(){

    if(distancia > $avioes['$a']['Alcance']){
        echo "Aviao com alcance inferior ao destino. VAI CAIR ";
    } else {

    $assentos = ($avioes['$a']['Assentos'] * 2) * $viagens;

    }

}

?>

1 answer

0

You have to go through this array and store the values.

https://www.php.net/manual/en/control-structures.foreach.php

foreach ($avioes as $codigo => $aviao) {
    // $codigo -> 'A220-100'
    // $aviao -> array('Aeronave'=>'A220-100', 'Fabricante'=>'Airbus', 'Ano'=> 2016, 'Assentos'=> 133, 'Carga'=> 13.3, 'Velocidade'=>850, 'Alcance'=>4075, 'Consumo'=>2.85, 'Desgaste'=>1.3, 'Preco'=>52.500000)
}

I posted comments on how you’ll be able to access this information. Another point, you can declare an array using only [ ] instead of array() according to the example below, it is more readable :)

$avioes = [
'A220-100' => ['Aeronave'=>'A220-100', 'Fabricante'=>'Airbus', 'Ano'=> 2016, 'Assentos'=> 133, 'Carga'=> 13.3, 'Velocidade'=>850, 'Alcance'=>4075, 'Consumo'=>2.85, 'Desgaste'=>1.3, 'Preco'=>52.500000],
'737-500' => ['Aeronave'=>'737-500', 'Fabricante'=>'Boeing', 'Ano'=> 1990, 'Assentos'=> 132, 'Carga'=> 15.2, 'Velocidade'=>786, 'Alcance'=>4725, 'Consumo'=>3.23, 'Desgaste'=>2.3, 'Preco'=>77.500000]
];

Browser other questions tagged

You are not signed in. Login or sign up in order to post.