How do I combine 2 arrays using their keys as the final array's key?

I have two arrays of checkbox content - the first is checked boxes:

$checked[$checkboxID] = “<input type=‘checkbox’ checked>”;

Second array contains the remaining unchecked boxes:

$unchecked[$numbersNotChecked] = “<input type=‘checkbox’>”;

I need to combine these into one array so I can sort them using the keys I’ve entered above - but combine will not assign the current keys as keys in the new array - for example:

If the above result is: $checked is key 1 and key 3 and $unchecked is key 2

$sortedarray = $unchecked + $checked;

will not sort $checked, $unchecked, $checked
it will create it’s own key and sort as $checked, $checked, $unchecked


Use array_merge to combine $checked and $unchecked, then use [url=]ksort to sort the combined array by key.

I tried array_merge but I’m not having any success with it. Here’s the actual code that I used for my 2 arrays:

$checked[$checkboxID] = array(“<input type=‘checkbox’ checked ‘value=’$checkboxID’><br>”;
$unchecked[$numbersNotChecked] = “<input type=‘checkbox ‘value=’$numbersNotChecked’><br>”;

The key numbers I need are actually in $checkboxID and $numbersNotChecked.

I don’t understand how the syntax works to assign these as keys. Something like this but this doesn’t work because I’m trying to assign the key as itself.

$checked = array($categoryID => $categoryID, “<input type=‘checkbox’ checked ‘value=’$categoryID’><br>”);

I presume the array_merge should look like this:
$checkboxes = array_merge((array)$checked, (array)$unchecked);

Or do I keep my 2 arrays as they are and somehow use the keys in the array_merge?

well first of all you’ve got a syntax error on your first line (didnt close your array() call)

The array syntax is this:
$array = array([[key =>] value] [, [key =>] value]* )

So for your example, it should be something like…
$checked = array($categoryID => “<input type=‘checkbox’ checked ‘value=’$categoryID’><br>”);

The syntax error is due to me trying so many different formats - in fact I see I completely messed it up and posted two different formats for each array - when they should have been the same. I also forgot to change $categoryID in my haste to make it consistent with my first post. So here goes. As originally posted at the top, it should be:

$checked[$checkboxID] = “<input type=‘checkbox’ checked>”;
$unchecked[$numbersNotChecked] = “<input type=‘checkbox’>”;

Now for the array_merge - this is giving me a syntax ) error which I dont’ see, and what’s the purpose of the asterisk?

$checkboxes = array([[key =>] $checked] [, [key =>] $unchecked]* );

I need to add the value of each array as well. Where does it go?


Asterisk is notation for 0-‘infinite’ (Though there is a limit, if you hit that limit you’ve got other troubles :wink: ) When i give the notation, means ‘optional’.

So all of these are valid:
$array = array();
$array = array(1,2,3);
$array = array(‘one’ => 1, ‘two’ => 2, ‘three’ => 3);
$array = array(1,‘two’ => ‘cow’, 3=>‘mreh’,4);

What is contained in $numbersNotChecked ? It sounds like thats a plural value, which wont work in the $array[$key] syntax.

No they are all single numbers - I’m trying to give them logical names - they’re the numbers of the checkboxes that are not checked - but they are entered into the array one at a time.

I understand the theory just can’t figure out how to apply it to my case. The numbers I want to use as keys are in variable names - but when I cast it as an integer it works in the original array. So for testing I have 3 boxes:

$checked array contains two numbers “1” and “3” each with the same value entered as follows:
[$checkboxID] plus there is a value “<input type=‘checkbox’ checked>”;

… so it runs correctly as
1, <input type=‘checkbox’ checked>
3, <input type=‘checkbox’ checked>

$unchecked array contains only one number “2” entered as key in [$numberNotChecked ] - I made it singular for you :wink: and this value “<input type=‘checkbox’>”;

… so it runs correctly as
2, <input type=‘checkbox’>

In the merged array I want to get this:

1, <input type=‘checkbox’ checked>
2, <input type=‘checkbox’>
3, <input type=‘checkbox’ checked>

So I need to use those original key numbers as keys to sort the final array (I know there is no zero which complicates it as well).

I thought you had to enter key values first but not according to your last example. This is where I’m getting lost… I need to merge the arrays together in the above order, but I can’t hard code 1, 2, 3 and this is not corrrect.

$array = array(
$checkboxID, => <input type=‘checkbox’ checked>,
$numberNotChecked => <input type=‘checkbox’>

Okay, lets try it this way. Show me how you’re filling $checked and $unchecked, and i’ll tell you how to merge them.

OK here’s all the code as I now have it and it gives the correct result. Category is just the name of this group of checkboxes.



   $checked = $_POST['category'];			
   $checkboxNum = $_POST['count'];
   $checkboxNum = $checkboxNum-1;
   $counter = 0;			

   //put the checked ID numbers into $checked array
   foreach ($checked as $checkboxID) {	

   //cast the variable to an integer to use as array key
   $checkboxID = (integer)$checkboxID;
   $checked[$checkboxID] = "<input type='checkbox' checked 'value='$checkboxID'>";			
   $counter = $counter+1;	

echo "checked "; print_r ($checked); echo "<br>";

//put the total number of categories into an array to loop through each one later

$boxNum = array();

//get the $checkedNum - the total number of categories

$checkedNum = $_POST['count'];
$checkedNum = $checkedNum-1;
$counter = 0;
while ($counter < $checkedNum){
   $checkNum = $counter+1;					
   $boxNum [] = $checkNum;				
   $counter = $counter + 1;				

//for each of the total category numbers
foreach ($boxNum as $numberBoxs){
   foreach ($checked as $checkboxID){

   //if the checked number is the same as one of the total category numbers
      if ($checkboxID == $numberBoxs){						
      //remove that number using $boxNum by its value with array difference						
      $boxNum = array_diff($boxNum, array($checkboxID));						

//what's left is unchecked
$unchecked = array();

foreach ($boxNum as $numberBox){	
   //cast the variable to an integer to use as array key
   $numberBox = (integer)$numberBox;
   $unchecked[$numberBox] = "<input type='checkbox' 'value='$numberBox'><br>";
   echo "Unchecked ".$numberBox."<br>";	

Okay. I’m trying to wrap my head around this. See if i’ve got this right.

Your post values contain ‘category’, which is an array of category ID’s that should be checked.
It also contains ‘count’, which is the total number of categories.
Your intention is to create an array of HTML tags which have a key of 0…count and the corresponding checked/not-checked HTML.

$count = (isset($_POST['count'])) ? (int) $_POST['count'] : 0;
$span = range(0,$count);
$html = array();
foreach($span as $key) {
  $html[] = "<input type='checkbox' value='$key' ".((in_array($_POST['category'],$key)) ? 'checked' : '').">";

should… do what you wanted?

I think you have the concept correct. It’s actually 3 checkboxes:


text description [HTML box 1]
text description [HTML box 2]
text description [HTML box 3]

I want to put them back in the same order no matter which ones have been checked.

the $html line is giving this error:

Warning: in_array() expects parameter 2 to be array, integer given

Er yeah. got my parameters backwards there. Should have been $key,$_POST[‘category’]


Thanks so much. You’re a hero.

One final note I forgot to add. I had to change the loop to run like this.

$span = range(1,$count-1);

Because the the HTML boxes start at 1 not 0.

Thanks again.