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
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]
Link to comment
Share on other sites

[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]
Link to comment
Share on other sites

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
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.