Error using array_push

370 Views Asked by At

I am trying to use array_push but I am recieving error messages like:

Warning: array_push() expects parameter 1 to be array, string given in C:\Users\DMR\Google Drive\android\maquetas\show.php on line 50

in two linews where I am using array_push, I don't understand why, could you help me please? the code is the next:

...

$etiquetes = array("N.I.F.", "Direcció");
$tipus = array("varchar", "varchar");
$columnes = array("CIF_NIF", "DIRECCION");
$llongituds = array(30, 30);

...

$i=0;
foreach ($etiquetes as $etiqueta) {
    $control = array_push($etiqueta, $columnes[$i], $tipus[$i], $llongituds[$i]);       <==== IT GIVE ME ERROR (ATTACHED AT THE END)
    $controls[$i % 2] = array_push($control); <==== IT GIVE ME ERROR (ATTACHED AT THE END)

    $i++;
}
2

There are 2 best solutions below

3
On

$etiqueta is not an array thus

 array_push($etiqueta, ...

is wrong. You might be looking for:

 array_push($etiquetes, ...

Also, from array_push() docs:

Returns the new number of elements in the array.

Which means $control will have an int value. So you second line

array_push($control);

Is ofcourse invalid, I would suggest you look at the docs and try to figure out what are you planning to do.

0
On

Here it is better to answer because it will be more clear for all of you (I hope)

I will explain here my solution, I have two DIV left and right and I have n controls in the array, what I am trying to do is when I loopp in the array I set the impair in the left and the pais in the right, for this reason I used the bucle:

foreach ($etiquetes as $etiqueta) {
     $control = array(array(), array(), array(), array());
     array_push($control, $etiqueta, $columnes[$i], $tipus[$i], $llongituds[$i]);       
     array_push($controls[$i % 2], $control);       

     $i++;
}

Now I have it it will be easy to set in the html code. The code I set before $columnes will be the fields of the table in the database and $etiquetes will be the label.

then I will need to put the length, type for the input fields, etc...

then the screen will be wllformed.

I hope it can be more clear for you. Now code I wrote here it is working just I have to put performance.