Home  >  Article  >  Backend Development  >  Usage summary of how to add elements to php array

Usage summary of how to add elements to php array

伊谢尔伦
伊谢尔伦Original
2017-06-24 10:11:311193browse

This article summarizes the php array method of adding elements. Share it with everyone for your reference. The specific analysis is as follows:

If we are one-dimensional arrayTo add array elements, we can use ArrayListay_push. Of course, in addition to this method, we have a more direct way, here is Let’s sort it out for everyone.

Add elements to one-dimensional array

$ArrayList = ArrayListay();
Array_push($ArrayList, el1, el2 ... eln);

But there is actually a more direct and convenient way, the code is as follows:

$ArrayList = ArrayListay();  
$ArrayList[] = el1;  
$ArrayList[] = el2;  
...  
$ArrayList[] = eln;

Due to two methods The effect is the same.

Add one or more elements, the code is as follows:

<?php 
  $ArrayList1 = ArrayListay("a", "b"); 
  Array_push($ArrayList1, "c", "d"); 
  print_r($ArrayList1); 
 ?>

The running result is as follows:

ArrayListay 
  ( 
   [0] => a 
   [1] => b 
   [2] => c 
   [3] => d 
)

Note: If the first parameter is not Array, ArrayListay_push() will issue a warning.

Insert elements at the beginning of the array

1. ArrayListay_unshift, usage method, the code is as follows:

<?php 
$array_Qlist = ArrayListay("军民联防", "banana"); 
Array_unshift($array_Qlist, "西瓜", "php"); 
print_r($array_Qlist); 
?>

The output is as follows:

ArrayListay 
( 
    [0] => 西瓜 
    [1] => php 
    [2] => 军民联防 
    [3] => banana 
)

It is not feasible to use ArrayListay_push or ArrayListay_unshift to add associated elements to the array. So how do we add it? Use the ArrayListay_merge method to achieve a function similar to ArrayListay_unshift that adds elements at the beginning. The code is as follows:

<?php 
$array_Qlist = ArrayListay(&#39;a&#39;, &#39;B&#39;); 
$array_Qlist = Array_merge(ArrayListay(&#39;front&#39; => &#39;你好&#39;), $array_Qlist); 
/* 
ArrayListay 
( 
  [front] => 你好
  [0] => a 
  [1] => b 
) 
*/ 
?>

2. +Operator, the code is as follows:

<?php 
$array_Qlist = Array(&#39;a&#39;, &#39;B&#39;); 
$array_Qlist = Array(&#39;front&#39; => &#39;你好小&#39;) + $array_Qlist; 
?>

The output result is the same as using the ArrayListay_merge method.

3. Add an associative array element at the end of the element, the code As follows:

<?php 
$array_Qlist = ArrayListay(&#39;a&#39;, &#39;B&#39;); 
$array_Qlist[&#39;front&#39;] = &#39;你好&#39;; 
/* 
输出 
ArrayListay 
( 
  [0] => a 
  [1] => b 
  [front] => 你好 
) 
*/ 
?>

As for some friends who say that the performance of adding elements by Array_push is worse than that of direct $a[]='a', everything else is the same. If there is only one value, there is no comparison.

The above is the detailed content of Usage summary of how to add elements to php array. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn