Hi
Can you see somthing I am missing?
My form is passing arrays to the process and the process is producing
some errors. I cannot see any problems. In fact I have two other simular
foreach loops that are not creating errors.
Thanks

<?php
$fee2_choice = strip_tags(trim($_POST['fee2_choice']));
$fee2_choice = array_filter($fee2_choice);//line 150
$fee2_choice = ereg_replace("[)(.-]","",$fee2_choice);
    
$fee2_code = strip_tags(trim($_POST['fee2_code']));
$fee2_code = array_filter($fee2_code); //line 155
$fee2_code = ereg_replace("[)(.-]","",$fee2_code);
 
$fee2_unit = strip_tags(trim($_POST['fee2_unit']));
$fee2_unit = array_filter($fee2_unit); //line 160
$fee2_unit = ereg_replace("[)(.-]","",$fee2_unit);
    
$fee2_describe = strip_tags(trim($_POST['fee2_describe']));
$fee2_describe = array_filter($fee2_describe);//line 165
$fee2_describe = ereg_replace("[)(.-]","",$fee2_describe);
 
$fee2_money = strip_tags(trim($_POST['fee2_money']));
$fee2_money  = array_filter($fee2_money); //line 170
$fee2_money = ereg_replace("[)(.-]","",$fee2_money);
    
//loop through arrays that will be inserted into database
$indices = array_keys($fee2_code); //line 391
foreach($indices as $index) //line 392
       {
   $fee2_choice[$index];
   $fee2_code[$index];//fee_code
   $fee2_unit[$index];//number of units
   $fee2_describe[$index];//description
   $fee2_money[$index];//service fee 
       
          //evaluation and insert occur here
       }
       
?>

error messages received when form is submitted:
Warning: array_filter() [function.array-filter]: The first argument should be an array in D:\search_process.php on line 150
Warning: array_filter() [function.array-filter]: The first argument should be an array in D:\search_process.php on line 155
Warning: array_filter() [function.array-filter]: The first argument should be an array in D:\search_process.php on line 160
Warning: array_filter() [function.array-filter]: The first argument should be an array in D:\search_process.php on line 165
Warning: array_filter() [function.array-filter]: The first argument should be an array in D:\search_process.php on line 170
Warning: array_keys() [function.array-keys]: The first argument should be an array in D:\search_process.php on line 391
Warning: Invalid argument supplied for foreach() in D:\pulse_web_server\webroot\pulse\billing\billing_ohip_fee_search_process.php on line 392
:eek:

Recommended Answers

All 8 Replies

array_filter() takes two arguments you only gave it one.

array_filter() takes two arguments you only gave it one.

Hi

Thanks for responding.

It is difficult to understand what the callback is can you explain better than the PHP manual?

Note: I was looking at Example 2. array_filter() without callback.

In that case you need to declare the variable you pass to it an array before passing it to that function.

$fee2_choice = new array();

In that case you need to declare the variable you pass to it an array before passing it to that function.

$fee2_choice = new array();

Thanks for the response.

If I am already working with arrays do this?

$fee2_choice = new array($fee2_choice);

The information coming from the form are arrays, see simple example.

//FORM FUNCTION
<?php
function name()
{
<tr> 
<td name"\fee2_choice[]\"></td>
<td name"\fee2_code[]\"></td>
</tr>
 
$all_array = array(fee2_choice, fee2_code);
returned $all_array;
}//end of function
 
list($fee2_choice, $fee2_code) = $all_array;
?>

Propose change

//PROCESS
<php>

$fee2_code = strip_tags(trim($_POST['fee2_code']));

$fee2_choice = new array($fee2_choice);
 
$fee2_code = array_filter($fee2_code);
 
 
</php>

A TD can't be an array. Only form elements like INPUT.

A TD can't be an array. Only form elements like INPUT.

That is correct. My mistake. This is the longer version of the code in the form function that dynamically create 20 to 49 rows.

echo"<tr height=\"10\">
       <td width=\"4%\" bgcolor=\"#eeeee0\" align=\"center\"><input  
          type=\"checkbox\" name=\"fee2_choice[$i]\" 
           value=\"$code_id\"></td>
       <td width=\"7%\" bgcolor=\"#eeeee0\" ><span class=\"style20
           \"><strong>$fee2_code</strong></span></td>
       <td width=\"3%\" bgcolor=\"#fff8dc\" height=\"10\">
          <input type=\"text\" name=\"fee2_unit[$i]\" size=\"1\" 
           maxlength=\"2\" value =\"$fee_unit\"/></td>
       <td width=\"79%\" bgcolor=\"#fff8dc\" class=\"style20\"> 
             $description </td>
       <td width=\"6%\" align=\"left\" >$p_fee</td>\n";
 echo"</tr>\n";

In your code, try dumping the cotents of the array before you use array_filter().
EG:

var_dump($fee2_code);
$fee2_code = array_filter($fee2_code); //line 155

The error is because it isn't an array. Even though your HTML looks ok and it seem it should be an array.


It is difficult to understand what the callback is can you explain better than the PHP manual?

Note: I was looking at Example 2. array_filter() without callback.

The callback is a function that you define, that will be executed in another function.

If you were to implement callback functions in your own code, it could look something like:

/**
* Example of function using a callback. (same as array_filter except it will filter out empty strings also)
* @param array The array to filter
* @param function The callback function
*/
function my_array_filter($array, $callback = false) {

	if (!$callback) {
		$callback = 'trim'; // default callback function: trim()
	}

	$new_array = array(); // filtered array
	foreach($array as $index=>$value) {
		// execute the callback function passing it the argument: $value
		if (call_user_func($callback, $value)) {
			$new_array[$index] = $value;
		}
	}
	return $new_array;
}

/**
* Example of my_array_filter()
*/

$arr = array('hey', false, true, '0', ' ', '');

var_dump(my_array_filter($arr));
echo '<br />';
var_dump(array_filter($arr));

If you look at the function, my_array_filter, there is a line:

call_user_func($callback, $value)

call_user_function will execute a user defined function. Since this function name is passed as an argument in to a function, then it is called a callback.
It just means its a chosen function to use inside another function...

Actually that explanation came out weird.

A simple example:

function my_callback_implement($callback, $argument) {

// some useful code here

$result = $callback($argument);

}

// example use


my_callback_implement('echo', 'This was echoed with echo() where echo is the callback function in my_callback_implement().');

Here the argument you suply is executed as the callback function on teh line:
$result = $callback($argument);

as you can see its just the variable you supply, being called as a function. A callback function..


Anyways, back to the array_filter.

If you dont define the second argument in array_filter then it uses a callback function that just returns the value of the argument supplied.
So if the argument is false, it will be filtered out.

Edit:

Note:

$callback = 'echo';
$argument = 'Hello callback world';
$callback($argument);

looks exactly like:

echo('Hello callback world');

to php.

However we call 'echo' the callback function since it is user defined, and called at runtime.

commented: excellent post - stymiee +2

Wow. Great explaination.
Thanks for the time and effort I will give it a shot.

"The best part of what I do are the problems. I then learn about the problem and how to solve it."

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.