synfield Posted March 18, 2008 Share Posted March 18, 2008 Hi, Basically this script passes an array ($arr) to a function and checks if any of the array values matches a key in a predefined array ($arrTxtFields), set within the function, if it does it checks the remaining passed array indices (1 & 2) for any values. if any of these values is empty the function should return false, if both indices have values e.g. $arr3, then the function should return true. However I can't get the function to return correctly, hoping someone can offer some guidance. function checkother($elements, $recursive = 0) { static $elementarr; echo "<br>\$recursive is set as : ".$recursive."<br>"; if(empty($recursive)){ //need to keep copy of original array $elementarr = $elements; } $arrTxtFields = array("cheq" => array("cheqno", "cheqamt"), "cardtype" => array("cardno", "expirydate"), "purchorderno" => array("orderno"), "invoice" => array("attn")); print_r($arrTxtFields); echo "<br><br>"; print_r($elements); echo "<br><br>"; echo "original elements array (\$elementarr) is :<br>"; print_r($elementarr); echo "<br>"; foreach($elements as $key => $value){ echo "\$recursive is $recursive \$key is : $key \$value is : $value <br>"; //if($key == 0){ if (is_array($value)) { /* foreach($value as $key2 => $value2){ echo "\$key2 is : $key2 \$value2 is : $value2 <br>"; */ $recursive ++; echo "<br>\$recursive incremented to: $recursive <br>"; checkother($value, $recursive); break; }else{ if(array_key_exists($value, $arrTxtFields)){ echo "<br>count(\$elementarr) is : ".count($elementarr)."<br>"; for ($i=1; $i < count($elementarr); $i++){ echo "<br> empty(\$elementarr[$i]) is : ".empty($elementarr[$i])."<br>"; echo "<br> empty(\$arrTxtFields[\$value][$i-1]) is : ".empty($arrTxtFields[$value][$i-1])."<br>"; if (empty($elementarr[$i]) && !empty($arrTxtFields[$value][$i-1])){ echo "<br> \$arrTxtFields[\$value][\$i] is : ".$arrTxtFields[$value][$i-1]."<br>"; $txtelmntname = $arrTxtFields[$value][$i-1]; echo "<br>"; echo "\$txtinputname is : ".$txtelmntname."<br>"; return false; }//end if (empty.. }//end for //return $return; }//end if (array.. }//end if (is_arr.. }//end foreach return true; } $arr = array(0 => array("cardtype"), 1 => '', 2 => ''); $arr2 = array(0 => array("cardtype"), 1 => '060108-465986-00 ', 2 => ''); $arr3 = array(0 => array("cardtype"), 1 => '060108-465986-00 ', 2 => '125.00'); //print_r($arr); $bln = checkother($arr2, $recursive = 0); echo "<br>\$bln is : $bln<br>"; Cheers. Quote Link to comment Share on other sites More sharing options...
Cep Posted March 18, 2008 Share Posted March 18, 2008 I think this should work, its a much less complicated version of what you have written so you may want to add your stuff in if you find it usefull <?php $arr = array(0 => array("cardtype"), 1 => '', 2 => ''); $arr2 = array(0 => array("cardtype"), 1 => '060108-465986-00 ', 2 => ''); $arr3 = array(0 => array("cardtype"), 1 => '060108-465986-00 ', 2 => '125.00'); function myCheck($myarray) { $return = true; foreach ($myarray as $value) { switch ($value) { case "this": // match this then do something break; case "that:": // match this then do something break; case "the other": // match this then do something break; default: // if no matches do this if ($value==="") { // if empty value $return = false; } } } return $return; } $val = myCheck($arr3); var_dump($val); ?> Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.