You can essentially set it to whatever term you want but it will be generally easier if you use 1/0 or true/false and then ran the item as a boolean for sanitation purposes. It also has the added benefits of you not having to run it through a comparison in an if as you could simply just call the variable itself and it will output a boolean true/false result, something that will also be effective in the ternary operators used in the above example. In that case all you’d need to do is have a default variable set to false and then just set that variable in the loop to $value, or, alternatively, run it through a simple if/ternary check. Something simple like, which will force any $value it’s run against to be either true or false and as such shouldn’t be used outside of an if $key = specific key name unless you want to change all of the values:
$value = ($value !=True)?False:True;
(the above is essentially the same solution used in post 6 but I’d generally recommend not adding a loop if you don’t have to and would personally run this check in the loop that outputs the content itself as initializing loops is somewhat more intensive, so far as I understand it, than just adding to the stuff an existing loop already does particularly when they’re functionally the exact same loop)
or
IF($value !=True){
$value = False;
}else{
$value = True;
}
Additionally the reason you’re getting a number is in your html you’re setting the value as the key instead of as True, that’s the red section in the code. The value there can be whatever you want, it need not be $key, and actually probably should not be key, I’d image it should actually be $value since that would provide the value already assigned to that array if checked instead of manually changing it each time to key regardless of what the original or intended value is.
foreach($outsourceArray AS $key => $value) {
$checked = (isset($_POST['outsource']) && is_array($_POST['outsource']) && in_array($key, $_POST['outsource'])) ? 'checked' : '' ;
$optionOutput .= '<input type="checkbox" class="checkbox" name="outsource[' . $key .']" value=[COLOR="#FF0000"]"'. $key .'"[/COLOR] '. $checked .'>'. $value .'<br />';
}
Additionally if the whole array is run through empty() you can assign it default values if it returns true. Which means you’ll be able to act in a certain way if there is no active session.
Here’s one quick tutorial on arrays I found with a google search, most of what you’re seeing here is looping through an array as a method of handling the data in them. Most PHP books will contain a number of sections just dedicated to looping through arrays in addition to a number of sections to the defining of arrays and I’m sure sitepoint has some offerings in the books/ebooks section that would be useful, particularly since it’s an easy topic to get lost in while new and having constant reference like that around will be helpful. For starters you’ll want to think of it as a way of tabling data with multidimensional arrays being tables storing tables (since that’s essentially all an array is, a not an actual database temporary table which roughly resembles what a database table would in, say, phpMyAdmin on a cpanel server). Sessions are essentially arrays that are slightly less temporary than an array but slightly more temporary than a database so taking the time to learn about arrays will be extremely helpful in learning both session and database management (as those are basically permanent arrays and return results in the same way arrays do).