1

What I am trying to do is to push somme arrays into one array: The static form of my array is as following:

$Data = array
  (
    array('uid' => 1, 'Field Name' => 'xxxx', 'Field Values' => 'xxxx'),
    array('uid' => 2, 'Field Name' => 'xxxx', 'Field Values' => 'xxxx'),
    array('uid' => 3, 'Field Name' => 'xxxx', 'Field Values' => 'xxxx'),
  );

I want to obtain the same Data array I tried the next, but it didnt work:

   $Data = array();
   // $Columns is an array that contains the Field Names
  for ($i=0; $i < sizeof($Columns); $i++) {
    $newelement=array('uid' =>$i, 'Field Name' => $Columns[$i], 'Field Values' => 'xxxx');
    $Data = array_push($Data,$newelement);
     }

Is there a better way than using array_push(); ??

1
  • Why array_push is not good? Also you can use $Data[]=$newlement; Commented Nov 15, 2013 at 15:18

2 Answers 2

3

You could use this slightly shorter syntax:

$Data[] = $newelement;
Sign up to request clarification or add additional context in comments.

Comments

0
$Data[] = $newelement 

$Data[] has the same result as array_push, but mush better perform.

not only shorter syntax, but also more efficiency that there is no overhead of calling a function.

especially, array_push() will raise a warning if the first argument is not an array.

This differs from the $[] behaviour where a new array is created.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.