Jump to content

Help with foreach statement


Darkmatter5

Recommended Posts

Here's my code

<?php
  $param=$HTTP_GET_VARS['param'];
  $options=array("values"=>array(0=>"a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z","num","all"),
                 "text"=>array(0=>"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z","0-9","Show all"));
              
  foreach($options as $value) { echo "Values: $value[values], Text: $value[text]<br>"; }
?>

 

My desired output is:

  Values: a, Text: A

  Values: b, Text: B

  etc...

  Values: num, Text: 0-9

  Values: all, Text: Show all

 

What am I doing wrong?

Link to comment
https://forums.phpfreaks.com/topic/137065-help-with-foreach-statement/
Share on other sites

For loop would be better for this situation.

 

<?php
  $param=$_GET['param'];   $options=array("values"=>array(0=>"a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z","num","all"),                "text"=>array(0=>"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z","0-9","Show all"));
$cnt = count($options["values"]);
for ($i=0; $i<$cnt; $i++) {
    echo "Values: " . $options["values"][$i] . ", Text: " . $options["text"][$i] . "<br />";
}
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.