0

Hi..
I'm not sure whats the array_merge doing here in this code?
I've read the mysql manual,but i just not sure what it is really means,so hope somebody could clear it out for me.

By the way this is the code :

if (isset($_POST['submit'])) { // Form has been submitted.
    $errors = array();

    // perform validations on the form data
    $required_fields = array('username', 'password');
    $errors = array_merge($errors, check_required_fields($required_fields, $_POST));

    $fields_with_lengths = array('username' => 30, 'password' => 30);
    $errors = array_merge($errors, check_max_field_lengths($fields_with_lengths, $_POST));

if ( empty($errors) ) {
   ....continue coding
}

wheres the check_required_fields and check_max_field_lengths are the function being made which is :

function check_required_fields($required_array) {
   $field_errors = array();
foreach($required_array as $fieldname) {
 if (!isset($_POST[$fieldname]) || (empty($_POST[$fieldname]) && $_POST[$fieldname] != 0)) {
	$field_errors[] = $fieldname;
  }
     }
	return $field_errors;
}

function check_max_field_lengths($field_length_array) {
	$field_errors = array();
        foreach($field_length_array as $fieldname => $maxlength ) {
if (strlen(trim(mysql_prep($_POST[$fieldname]))) > $maxlength) { $field_errors[] = $fieldname; }
	}
	return $field_errors;
}

actually,is that array_merge function in this code combine the return values from
check_required_fields and check_max_field_lengths function??

Thank You for helping :)

Edited by samsons17: n/a

3
Contributors
8
Replies
9
Views
7 Years
Discussion Span
Last Post by samsons17
0

Both functions return an array of fields, where the field is not there or empty. The merge merges all results together, so if at the end the newly merged array is still empty, all fields are validated to have a value.

0

em..why if i do the merge like this my browser shows me the array is empty? :

<?php

$result=array();
$func2=array(1,2,3);
$func3=array(4,5,6);
$result=array_merge($result,$func2);
$result=array_merge($result,$func3);

echo $result;

?>
0

array_merge function in this code combine the return values from
check_required_fields and again merge the result of check_max_field_lengths function

So two arrays get merged into a single array.

Edited by rajarajan07: n/a

0

ya ya..
but why when i try to use the array_merge like this :

<?php

$result=array();
$func2=array(1,2,3);
$func3=array(4,5,6);
$result=array_merge($result,$func2);
$result=array_merge($result,$func3);

echo $result;

?>

..the result on my browser is just Array while i expecting it to echo all the array that i've merged..

0
$result=array();
$func2=array(1,2,3);
$func3=array(4,5,6);
$result=array_merge($func2,$func3);
print_r($result);

That should be like this

0

rajarajan07 : yes,your code and mine is just the same.

but see what happens when using echo instead of print_r..

the result on the browser will be just Array..

i just dont understand,if we could use array_merge() to merge both array,then why couldnt we output the result on the browser using the echo statement??

0

Not only for array_merge. Any Array will be print promptly using print statement only not with echo. print_r is a special keyword for display array value with their pairs.

Hope you clear! echo is to just print stirngs and parse your variables not arrays.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.