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];
}
?>
<?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'];
?>
I don’t understand, what is the purpose of the code?
– rray
It is possible
foreach(numeros_pequenos($zero, $um, $dois) as $item) echo $item .'<br>';
but it’s not the best way ...– rray
It would not be better to type: "$variavel = small numbers($zero, $one, $two);" And then foreach ($variable the $item). ?
– Fleuquer Lima
chorus to @rray scroll through the array for what ?
– Nelson Teixeira
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]; }
– Davi Sousa
See if this helps you: http://php.net/manual/en/function.chunk-split.php
– Vítor André
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']
– rray
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.
– Davi Sousa
that
$key[0]
forget it, just leave it$key
– rray
but it returns 987 I only want the 9, IE, for more that returns 3 different values, I only want a specific.
– Davi Sousa
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.
– rray
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.– Armando Marques Sobrinho