选中复选框时如何组合两个数组[关闭]

问题描述:

I have two array and two check boxes. The array values are

  $a={1,2,3,4,5} --->It' for first Check box
  $b={5,6,7,8,9} --->It' for Second Check box

When I check the first check box only I want the result is

 $c[0]=1,$c[1]=2,$c[2]=3,$c[3]=4,$c[4]=5

And I check the second check box only I want the result is

$c[0]=5,$c[1]=6,$c[2]=7,$c[3]=8,$c[4]=9

And I have check both check boxes I want the result is

$c[0]={1+5},$c[1]={2+6},$c[2]={3+7},$c[3]={4+8},$c[4]={5+9}

It's Possible?

我有两个数组和两个复选框。 数组值为 p>

  $ a = {1,2,3,4,5} --->它'为第一个复选框
 $ b = {5  ,6,7,8,9} --->它'用于第二个复选框
  code>  pre> 
 
 

当我选中第一个复选框时,我只希望结果是 p>

  $ c [0] = 1,$ c [1] = 2,$ c [2] = 3,$ c [3] = 4,$ c [4  ] = 5 
  code>  pre> 
 
 

我只检查第二个复选框,我希望结果是 p>

  $ c [  0] = 5,$ C [1] = 6,$ C [2] = 7,$ C [3] = 8,$ C [4] = 9 
 代码>  PRE> 
 
  

我检查了两个复选框我希望结果是 p>

  $ c [0] = {1 + 5},$ c [1] = {2+  6},$ C [2] = {3 + 7},$ C [3] = {4 + 8},$ C [4] = {5 + 9} 
 代码>  PRE> 
  
 

这可能吗? p> div>

You can achieve this using a for() loop. Please note that this code assumes both arrays are the same size:

$c = array();
for($i = 0; $i < count($a); $i++)
{ 
    $c[] = ($a[$i] + $b[$i]);
}

print_r($c);

Yes you can do it following way

<?php
 $a = array(1,2,3,4,5);
 $b = array(5,6,7,8,9);
 $c = array();  //for result
   if(isset($_POST['checkbox_1'])){
     $c = $a;
   }
   else  if(isset($_POST['checkbox_2'])){
     $c = $b;
   } else if(isset($_POST['checkbox_1']) && isset($_POST['checkbox_2'])){
    if(count($a) == count($b)){
      for($i=0;$i<count($a);$i+}){
        $c[]=$a[i] + $b[i];
      }
   }
   }else{
    echo 'Error Contact Admin';
   }

?>