user2747249
user2747249

Reputation:

php: push multiple arrays in one array

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(); ??

Upvotes: 1

Views: 8431

Answers (2)

fayhot
fayhot

Reputation: 59

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

Upvotes: 0

Sirko
Sirko

Reputation: 74036

You could use this slightly shorter syntax:

$Data[] = $newelement;

Upvotes: 3

Related Questions