Question

I want to get the number of values that were submitted via my $_GET. I'm going to be using a for loop and every 7 are going to be saved to a class. Does anyone know how to find the Size_of the $_GET array? I looked through this info $_GET Manual, but don't see what I'm looking for.

This is a code snippet that I started in the class that I go to after the submit.

for($i=0;$i<$_GET<size>;$i++) {
    $ID = $_GET["ID"][$i];
    $Desc = $_GET["Desc"][$i];
    $Yevaluation = $_GET["Yevaluation"][$i];
    $Mevaluation = $_GET["Mevaluation"][$i];
    $Cevaluation = $_GET["Cevaluation"][$i];
    $Kevaluation = $_GET["Kevaluation"][$i];
    $comment = $_GET["comment"][$i];
    //saving bunch to class next
}
Was it helpful?

Solution

$_GET is an array. Like any other array in PHP, to find the number of elements in an array you can use count() or its alias sizeof().

OTHER TIPS

count($_GET["ID"])

could work. However, then you have to be sure that each index contains the same number of elements.

More secure would be

min(count($_GET["ID"]), count($_GET["Desc"]), ...)

With array_map this can probably be shortcut to something like:

min(array_map(count, $_GET))

Try thinking through the last one, if it seems difficult to you. It's called functional programming and it's an awesome way to shortcut some stuff. In fact array_map calls another function on each element of an array. It's actually the same as:

$filteredGet = array();
foreach ($element in $_GET) { // this is the array_map part
    $filteredGet[] = count($element);
}
min($filteredGet);

Even better would be a structure where $i is the first level of the array, but I think this is not possible (built-in) with PHPs $_GET-parsing.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top