Traverse an array of a php function

Asked

Viewed 172 times

0

I have this function, I would like to go through its values separately but I’m stuck here and I can’t think straight 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 AI FRIEND BELOW AND TO ALL WHO COOPERATED. 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'];

?>

  • 1

    I don’t understand, what is the purpose of the code?

  • It is possible foreach(numeros_pequenos($zero, $um, $dois) as $item) echo $item .'<br>'; but it’s not the best way ...

  • It would not be better to type: "$variavel = small numbers($zero, $one, $two);" And then foreach ($variable the $item). ?

  • chorus to @rray scroll through the array for what ?

  • Until then ok, what I’m not getting is to take just one specific item, like, I just want number 9, I’m not getting it. $variable = small numbers($zero, $one, $two); foreach ($variable as $key) { echo $key[0]; }

  • See if this helps you: http://php.net/manual/en/function.chunk-split.php

  • You need to explain better what your doubt is, this code doesn’t make much sense. vc can take an element of an array specifying the Dice or key, the syntax is $variavel[0] or $variavel['nome']

  • I edited the code to give you an idea of what I want. Or instead of printing all the values of the function, print one.

  • that $key[0] forget it, just leave it $key

  • but it returns 987 I only want the 9, IE, for more that returns 3 different values, I only want a specific.

  • If you want a specific item just put the Dice in the call as said in the other comment, indices start with zero, ie vc has three elements in total but count is zero to two.

  • then you’d better wear a for simple, something like:for ($i = 0;$<count($variavel);$i++){if $variavel[$i] == 9 break;/*...*/} dai Voce would have greater control over the loop.

Show 7 more comments

1 answer

1


I don’t know if I got it right, but it looks like you want to add the values through the function and then call a specific one. You may be doing it this way:

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 doesn’t make much sense because you could just be adding the values inside the array, but I 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
  • 1

    Hey, kid, that’s right, Valew. <? php Function numeros_small($zero, $one, $two) { Return array ('numero0'=>$zero, 'numero1' =>$one, 'numero2' => $two); } $zero = 9; $one=8;$two =7; $variable = small numeros_small($zero, $one, $two); echo $variable['numero0']; ?>

Browser other questions tagged

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