Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Warning: array_merge(): Argument #1 is not an array, when processing two $_POST

Tags:

arrays

php

I'm receiving the following error Warning: array_merge(): Argument #1 is not an array when processing $_POST['cpl'], although $_POST['add'] works fine

if (is_array($_POST['add'])) {
    foreach ($_POST['add'] as $key => $value) $_POST['add'][$key] = mysql_real_escape_string($value);
    $en = array_merge($en, $_POST['add']);
}

if (is_array($_POST['cpl'])) {
    foreach ($_POST['cpl'] as $key => $value) $_POST['cpl'][$key] = mysql_real_escape_string($value);
    $cp = '';
    $cp = array_merge($cp, $_POST['cpl']);
}
like image 666
acctman Avatar asked Sep 09 '13 19:09

acctman


2 Answers

That's because $cp is a string (you explicitly defined it that way).

$cp = ''; // <-- empty string
$cp = array_merge($cp, $_POST['cpl']);

should be:

$cp = array(); // <--now it's an array
$cp = array_merge($cp, $_POST['cpl']);
like image 144
John Conde Avatar answered Nov 18 '22 05:11

John Conde


You have these lines:

$cp = '';
$cp = array_merge($cp, $_POST['cpl']);

It's self-explanatory: $cp is a string first, the error is simply about this fact. Initialize it with array() instead.

like image 23
Sven Avatar answered Nov 18 '22 04:11

Sven