954,591 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Array filtering

Hi
Its back, I am being haunted.
I thought I had resolved this problem but I am intermittently
the receving the warnings below.
This code consist of a 1) HTML form, 2) a display function on the HTML form and
3) a process to manage the form input.
Can you show me what I am doing wrong and or how to correct it ?
Message received:
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 156
Warning: array_filter() [function.array-filter]:
The first argument should be an array in D:\search_process.php on line 171
Warning: array_filter() [function.array-filter]:
The first argument should be an array in D:\search_process.php on line 172

Note: I have proveded only a small portion of the related code.

[html]

{//function start
//for loop start
{

echo"

$fee1_code


$description
$s_fee\n";
echo"\n";
}//end of loop
//assign all arrays into single array for the return statement
$all_array = array(fee_choice, fee_unit, fee_money, fee1_choice, fee1_unit, fee1_fee, fee2_choice, fee2_code, fee2_unit, fee2_describe, fee2_money, fee3_choice, fee3_code, fee3_unit, fee3_describe, fee3_money);
//return array from function
return ($all_array);
}//end of function
//unpack returned array
list($fee_choice, $fee_unit, $fee_money, $fee1_choice, $fee1_unit, $fee1_fee, $fee2_choice, $fee2_code, $fee2_unit, $fee2_describe, $fee2_money, $fee3_choice, $fee3_code, $fee3_unit, $fee3_describe, $fee3_money )= $all_array;
[/html]

[/php]

I changed the format of the array returned by the function
and thoug thr problem was solved but I startr to get the errors again
[php]
<?
$all_array = array($fee_choice, $fee_unit, $fee_money, $fee1_choice,
$fee1_unit, $fee1_fee, $fee2_choice, $fee2_code, $fee2_unit, $fee2_describe,
$fee2_money, $fee3_choice, $fee3_code, $fee3_unit, $fee3_describe, $fee3_money);
?>
[/php]

[php]
<?php
/**PROCESS**/
/**data from form**/
$fee_unit = $_POST['fee_unit'];//array with the number of units
$fee_money = $_POST['fee_money'];//array selected fee
//filter blank indexes
$fee_unit = array_filter($fee_unit); //line 155
$fee_money = array_filter($fee_money); //line 156

/**data from form**/
$fee1_unit = $_POST['fee1_unit'];//array with the number of units
$fee1_money = $_POST['fee1_money'];//array selected fee

//filter blank indexes
$fee1_unit = array_filter($fee1_unit); //line 171
$fee1_money = array_filter($fee1_money);//line 172
/** get array contents for insertion**/
$indices2 = array_keys($code1_id);
foreach($indices2 as $index2)
{
//individual value validation from 3 arrays
$code1_id[$index2];
$fee1_unit[$index2];
$fee1_money[$index2];

//validate unit
$field_unit = $fee1_unit[$index2];//assign field to array for function
check_unit_field($field_unit);//funtion to validate user entered numbers or message sent

//insert statement goes here

}
?>
[/php]
I have added the code below to the process to get rid of
the errors but this creates another problem where the
foreach loop can't extract the data from the arrays.

[php]
<?
//create array A
$fee_unit = array($fee_unit);
$fee_money = array($fee_money);

//create array B
$fee1_unit = array($fee1_unit);
$fee1_money = array($fee1_money);
?>
[/php]

assgar
Junior Poster in Training
89 posts since Oct 2006
Reputation Points: 24
Solved Threads: 0
 

Try using the is_array() function which returns true only if the argument is an array. That way you can make sure you're only passing arrays to array_filter().

eg:

[php]
if (is_array($array)) {
$filtered_array = array_filter($array, 'filter_function_name');
}
[/php]
or maybe:
[php]
if (is_array($array)) {
$filtered_array = array_filter($array, 'filter_function_name');
} else {

// maybe its a string so make it an array
$array = array($array);
$filtered_array = array_filter($array, 'filter_function_name');

}
[/php]

digital-ether
Nearly a Posting Virtuoso
Moderator
1,293 posts since Sep 2005
Reputation Points: 461
Solved Threads: 101
 
Try using the is_array() function which returns true only if the argument is an array. That way you can make sure you're only passing arrays to array_filter(). eg: [php] if (is_array($array)) { $filtered_array = array_filter($array, 'filter_function_name'); } [/php] or maybe: [php] if (is_array($array)) { $filtered_array = array_filter($array, 'filter_function_name'); } else { // maybe its a string so make it an array $array = array($array); $filtered_array = array_filter($array, 'filter_function_name'); } [/php]

Hi

Thanks for the suggestion.

I have been using this bit of code to resolve the problem for over
a week and have not received the error message.
This can be considered resolved.

[php]
/*** arrays passed from the form with selected service variables ***/
$code_id = $_POST['fee_choice']; //array of code_id primary key
$fee_unit = $_POST['fee_unit'];//array with the number of units
$fee_money = $_POST['fee_money'];//array selected fee

//filter array $fee_unit
if(is_array($fee_unit))
{
$fee_unit = array_filter($fee_unit);
}
else
{
$fee_unit = array($fee_unit);
$fee_unit = array_filter($fee_unit);
}
//filter array $fee_money
if(is_array($fee_money))
{
$fee_money = array_filter($fee_money);
}
else
{
$fee_money = array($fee_money);
$fee_money = array_filter($fee_money);
}
[/php]

assgar
Junior Poster in Training
89 posts since Oct 2006
Reputation Points: 24
Solved Threads: 0
 

awesome..

digital-ether
Nearly a Posting Virtuoso
Moderator
1,293 posts since Sep 2005
Reputation Points: 461
Solved Threads: 101
 

Thanks the suggestion worked.

assgar
Junior Poster in Training
89 posts since Oct 2006
Reputation Points: 24
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You