Jump to content

[SOLVED] variables in a array


Maxraid

Recommended Posts

is it possible to insert variables into an array and then run trough those variables in the array one by one and makes some chances to them.

example:
[code]
$checkboxArray = array("$rigger","$anhugger","$systemfacade");
$max = sizeof($checkboxArray);

//Change the value of checkboxes 
for($counter=1; $counter<$max; $counter++) {
   
  if ($checkboxArray[$counter] == "on")
  { $checkboxArray[$counter] = "YES"; }
        else
        { $checkboxArray[$counter] = "NO"; }
}
[/code]
the if statement works if I do the variables one at the time but since i have 25 variables, I have to make the IF statement 25 times that cant be true?
Link to comment
https://forums.phpfreaks.com/topic/28965-solved-variables-in-a-array/
Share on other sites

this isnt tested... but it should work...
foreach is MUCH faster and easier with arrays :-)
[code]
<?
$checkboxArray = array( 1=>"$rigger", 2=>"$anhugger", 3=>"$systemfacade");

foreach($checkboxArray as $k => $v){
if($v == "on") $checkboxArray[$k] = "YES";
else $checkboxArray[$k] = "NO";
}
?>
[/code]
[quote author=taith link=topic=116821.msg476199#msg476199 date=1164894867]
this isnt tested... but it should work...
foreach is MUCH faster and easier with arrays :-)
[code]
<?
$checkboxArray = array( 1=>"$rigger", 2=>"$anhugger", 3=>"$systemfacade");

foreach($checkboxArray as $k => $v){
if($v == "on") $checkboxArray[$k] = "YES";
else $checkboxArray[$k] = "NO";
}
?>
[/code]
[/quote]

And the shortest way to do:

[code]
<?php
$checkboxArray = array( 1=>"$rigger", 2=>"$anhugger", 3=>"$systemfacade");
foreach($checkboxArray as $k => $v){
  $checkboxArray[$k] = $v == "on" ? "Yes" : "No";
}
?>
[/code]
Thats great guys its working, I just have one small question more.
Now i've change the values of variables in the array, but what if i want to print just one specifik one exam. nr 3 ;).
its because i need to display the result not in a long line, but specifik places in html?
echo($checkboxArray[?]);

Do u get my idea

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.