-2

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++;
}
Michael Irigoyen
  • 22,513
  • 17
  • 89
  • 131
Dave
  • 7,028
  • 11
  • 35
  • 58
  • read the warning message? $etiquata is a string, you're supposed to use $etiquets (because it's an array) – Jeroen Ingelbrecht Aug 25 '13 at 12:03
  • What are you trying to do in that loop? What are `$control` and `$controls`? – vee Aug 25 '13 at 12:04
  • 1
    Read the manual on [`array_push`](http://php.net/array_push). First parameter needs to be an array. It isn't. Reconsider what your code does. If you want help, curb the annoying pleas for help and instead elaborate on what your code was intended to to. – mario Aug 25 '13 at 12:04
  • I mistook the using of I array i dind't read the right format for array_push. Thanks. I read manual again maybe I thjought in another language the use of push... typical error – Dave Aug 25 '13 at 15:28

2 Answers2

2

$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.

Yotam Omer
  • 15,310
  • 11
  • 62
  • 65
  • $etiquetes, .. is the array and I want the value inside the array for this reason I used foreach. But ... thanks becuase I rereaded again about array_push and I saw my error. – Dave Aug 25 '13 at 15:26
  • `$control = array(array(), array(), array(), array()); array_push($control, $etiqueta, $columnes[$i], $tipus[$i], $llongituds[$i]); array_push($controls[$i % 2], $control); ` – Dave Aug 25 '13 at 22:37
  • It is working, I saw the error in the tutorial. As I said a litle mistake from me – Dave Aug 25 '13 at 22:37
0

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.

Dave
  • 7,028
  • 11
  • 35
  • 58