PHP Arrays and $_POST acting strange -
i have following html:
<input type="checkbox" name="plusfri[]" value="fri"> friday <input type="checkbox" name="plussat[]" value="sat"> saturday <input type="checkbox" name="plussun[]" value="sun"> sunday <input type="checkbox" name="plusmon[]" value="mon"> monday that posts following php:
$plus = array(array("name" => "", "days" => "", "age" => "","conc" => "")); foreach($_post['plusname'] $k => $p) { $plus[$k]['name'] = $p; $plus[$k]['age'] = $_post['plusage'][$k]; $plus[$k]['conc'] = $_post['plusconc'][$k]; $plus[$k]['days'] = "x"; if($_post['plusfri'][$k]=="fri") $plus[$k]['days'] .= "1"; if($_post['plussat'][$k]=="sat") $plus[$k]['days'] .= "2"; if($_post['plussun'][$k]=="sun") $plus[$k]['days'] .= "3"; if($_post['plusmon'][$k]=="mon") $plus[$k]['days'] .= "4"; } and end with:
array(3) { [0]=> array(4) { ["name"]=> string(6) "frisat" ["days"]=> string(3) "x12" ["age"]=> string(1) "1" ["conc"]=> string(0) "" } [1]=> array(4) { ["name"]=> string(6) "satsun" ["age"]=> string(1) "1" ["conc"]=> string(0) "" ["days"]=> string(3) "x23" } [2]=> array(4) { ["name"]=> string(6) "sunmon" ["age"]=> string(1) "1" ["conc"]=> string(0) "" ["days"]=> string(3) "x34" } } can tell me why 'days' has jumped end on elements [1] , [2] ??>
it because of loop. same key order, should set value in same order too, :
$plus = array(array("name" => "", "days" => "", "age" => "","conc" => "")); foreach($_post['plusname'] $k => $p) { $plus[$k]['name'] = $p; $plus[$k]['days'] = "x"; // <-- see ? $plus[$k]['age'] = $_post['plusage'][$k]; $plus[$k]['conc'] = $_post['plusconc'][$k]; if($_post['plusfri'][$k]=="fri") $plus[$k]['days'] .= "1"; if($_post['plussat'][$k]=="sat") $plus[$k]['days'] .= "2"; if($_post['plussun'][$k]=="sun") $plus[$k]['days'] .= "3"; if($_post['plusmon'][$k]=="mon") $plus[$k]['days'] .= "4"; }
Comments
Post a Comment