Return desirable amount

Asked

Viewed 36 times

-1

Good evening, how can I return values from an array according to the value of a variable ? I’ve tried this way (unsuccessfully):

The goal would be to return the desired amount (informed in the GET to return) of elements of an array

localhost/estudo.php?retornar=5
$retornar = $_GET['retornar'];
for ($i=0; $i < $retornar ; $i++) { 
$ccs = $ccs[$i];
echo $ccs;
}
  • Post the complete code, so I’m missing the array part.

  • 2

    No need to convert the $retornar for https://3v4l.org/Ke6Cu integer (as stated below). The error is that you are replacing the array $ccs on the line $ccs = $ccs[$i];

  • @Valdeirpsr I have incorporated your comment, but if you prefer to reply separately, and I withdraw from mine, that the +1 is guaranteed.

2 answers

4

The correct answer is colleague Valdeirpsr’s comment. You are spoiling the array in the loop:

for ($i=0; $i < $retornar ; $i++) { 
   $ccs = $ccs[$i];
   echo $ccs;       // Na segunda iteração, a linha anterior vai falhar
}

Simply rename the variable, or use the index already in the echo:

for ($i=0; $i < $retornar ; $i++) { 
   $ccs2 = $ccs[$i];
   echo $ccs2;
}

Alternative

If it were not an exercise, but a real application, you could use what is already ready in PHP, in one line:

print_r( array_slice( $ccs, 0, $_GET['retornar'] );

Or even

$pedaco = array_slice( $ccs, 0, $_GET['retornar'] );
foreach ($pedaco as $key=>$value) { 
   echo $value;
}

See working on IDEONE


The array_slice returns a array extracted from another, determined by a offset and a length.

Handbook:

https://secure.php.net/manual/en/function.array-slice.php

1


First of all, I don’t recommend using this type of model, unless for a case study.

Second, even if it is for study I recommend applying some logic:

  1. Iterate only if the return is greater than one (1)
  2. Check that the value passed does not exceed the number of items
  3. Convert to inteiro, guaranteed that the user enters a valid number
  4. Check if the value passed is a number

Initially, applying according to your code, follows updated model;

$retornar = 5; //(int)$_GET['retornar'];

$ccs = array("1", "2"); //código novo (já que você não postou a linha referente ao array)

for($i = 0; $i < $retornar; $i++) { 
    echo $ccs[$i];
}

See in operation https://3v4l.org/YOSLU

Changing to recommended form

I use 0 to check if 1 has been selected, because the array starts at 0, so just change the loop to 1

<?php

$retornar = 5; //$_GET['retornar'];

$ccs = array("1", "2");

if($retornar > count($ccs)){
    die("Quantidade indisponivel");
}

if($retornar == 0){
    echo $ccs[0];
}else{
    for($i = 0; $i < $retornar; $i++) { 
        echo $ccs[$i];
    }
}

See in operation https://3v4l.org/ntcpZ

This new form is not 100% related to the information, is for you to try to do, since the topic itself has already been answered.

  • 1

    No need to convert the $retornar for https://3v4l.org/Ke6Cuinteger

  • It worked perfectly, thank you.

Browser other questions tagged

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