Go through the value of an array of a php function [closed]

0

I have this function, I would like to go through the values of it separately but I have here and I'm not able to reason right to do a foreach for example.

<?php
function numeros_pequenos($zero, $um, $dois)
{
return array ($zero, $um, $dois);
}
$zero = 9; $um=8;$dois =7;
$variavel = numeros_pequenos($zero, $um, $dois);
foreach ($variavel as $key) {
echo $key[0];

}
?>
RESOLUTION THANKS TO THE FRIEND AI IN LOW AND TO ALL THOSE WHO COLLABORATED. THANK YOU VERY MUCH.

<?php 

function numeros_pequenos($zero, $um, $dois) {
return array (
'numero0'=>$zero,
'numero1' =>$um,
'numero2' => $dois); 
} 
$zero = 9; $um=8;$dois =7; 
$variavel = numeros_pequenos($zero, $um, $dois); 
echo $variavel['numero0'];

?>
    
asked by anonymous 10.05.2016 / 21:38

1 answer

1

I do not know if I understand it right, but it seems like you want to add the values through the function and then call a specific one. You may be doing this:

function numeros_pequenos()
{
    return func_get_args(); // retorna todos os parâmetros passados
}

// variáveis
$zero = 9;
$um = 8;
$dois = 7;

$variavel = numeros_pequenos($zero, $um, $dois);

echo $variavel[0]; // 9

// ou

echo numeros_pequenos($zero, $um, $dois)[0]; // 9

The code does not make much sense since you might just be adding the values inside the array, but I've tried to answer your question.

$variavel = array(9, 8, 7);
echo $variavel[0]; // 9
// ou
$variavel = array(
    "zero" => 9,
    "um" => 8,
    "dois" => 7
);
echo $variavel["zero"]; // 9
    
10.05.2016 / 22:14