Print something only when the value is inside a specific group during a loop in PHP

I have a large array that is used throughout the site and only changes its structure for the next task - it's a pain. I want to make multiple boxes with a different array. The result I want to get looks like this:

/* Example Array:

  $allAnimals = array("Frogs","Toads","Bats","Elephants","Rats","Seals",
                      "Crocodilians","Turtles");

  $group = array("Frogs"=>"Amphibian","Bats"=>"Mammal","Crocodilians"=>"Reptile");
*/

<select name='Amphibian'>
   <option value='0'>Frogs</option>
   <option value='1'>Toads</option>
</select>

<select name='Mammal'>
   <option value='2'>Bats</option>
   <option value='3'>Elephants</option>
   <option value='4'>Rats</option>
   <option value='5'>Seals</option>
</select>

<select name='Reptile'>
   <option value='6'>Crocodilians</option>
   <option value='7'>Turtles</option>
</select>

      

I cannot figure out how to print the parameters only when the value is within a certain group of animals during each iteration $group

. I tried each()

to get the next animal $endAnimal

from $group

and then break the inner loop if it $animal

matches $endAnimal

, but I also need to make the loop start print parameters with specific values.

<?php

 $allAnimals = array("Frogs","Toads","Bats","Elephants","Rats","Seals","Crocodilians","Turtles");

$group = array("Frogs"=>"Amphibian","Bats"=>"Mammal","Crocodilians"=>"Reptile");

foreach($group as $thisAnimal=>$category){

   $nextKey = (each($group));

   $endAnimal = $nextKey['key'];

   print "<select name='$category'>";

   foreach($allAnimals as $idx=>$animal){

      print "<option value='$idx'>$animal</option>";

      if($endAnimal === $animal){
          break;
      } 
   }

   print "</select>";

}

?>

      

+3


source to share


3 answers


Please check it: -

 <?php

 $allAnimals = array("Frogs","Toads","Bats","Elephants","Rats","Seals","Crocodilians","Turtles");

$group = array("Frogs"=>"Amphibian","Bats"=>"Mammal","Crocodilians"=>"Reptile");

$group_keys = array_keys($group); // get the keys of group array
$j = 0;
foreach($group_keys as $key => $group_k){
   print "<select name='$group[$group_k]'>";
   if(isset($group_keys[$key+1])){
    $new_value = $group_keys[$key+1];
   }else{
       $new_value = '';
   }
   if($new_value ==''){
      foreach($allAnimals as $key => $allAnm){
        print "<option value='$j'>$allAnm</option>";
        unset($allAnimals[$key]);
        $j ++;
      }
   }else{

        $key_from = array_search($new_value,$allAnimals);

       for($i = 0; $i<$key_from; $i++){
            print "<option value='$j'>$allAnimals[$i]</option>";
            unset($allAnimals[$i]);
            $j ++;

       }
   }

   $allAnimals = array_values($allAnimals);
   print "</select>";
}

      



Result: - https://eval.in/379462 (eval.in)

local end: - (for better showing): - http://prntscr.com/7fmtbi

+1


source


Array array is the solution I believe



$group = array( "Amphibian" => array("Frogs", "Toads"), "Mammal" => array("Bats", "Elephants", "Rats", "Seals"), "Reptile" => array("Crocodilians", "Turtles") );

foreach($group as $thisAnimal=>$category){

   print "<select name='$category'>";

   foreach($category as $idx=>$animal){
      print "<option value='$idx'>$animal</option>";
   }
   print "</select>";

}

      

+1


source


Thanks for your responses and responses. I just like to work, checking, whether $thisAnimal

from $group

the same as that $animal

of $allAnimals

in the inner loop. If it's the same, turn $passThis

in true

and then go to print the option until $endAnimal

it matches $animal

. Example

<?php
$allAnimals = array("Frogs","Toads","Bats","Elephants","Rats","Seals","Crocodilians","Turtles");

$group = array("Frogs"=>"Amphibian","Bats"=>"Mammal","Crocodilians"=>"Reptile");

foreach($group as $thisAnimal=>$category){

   $nextKey = (each($group));

   $passThis = false;

   $endAnimal = $nextKey['key'];

   print "<select name='$category'>";

   foreach($allAnimals as $idx=>$animal){

      if($animal === $thisAnimal){
         $passThis = true;
      } 

      if($endAnimal === $animal){
          break;
      } 
      if($passThis === true)
      {   
         print "<option value='$idx'>$animal</option>";
      } 

   }

   print "</select>";

}

?>

      

+1


source







All Articles