php array operations (review)

php array operations (review)

1. Combine arrays

The array_merge() function merges the arrays together and returns a combined array. The resulting array starts with the first input array parameter and is forced to add sequentially in the order in which the following array parameters appear. Its form is:

Php code

  1. array array_merge (array array1 array2…,arrayN)

This function combines the elements of one or more arrays, and the values ​​in one array are appended to the back of the previous array. Return the resulting array.

If there is the same string key name in the input array, the value after the key name will overwrite the previous value. However, if the array contains numeric key names, the subsequent value will not overwrite the original value, but will be appended to the back.

If only one array is given and the array is numerically indexed, the key names will be re-indexed in a continuous manner.

Php code

  1. <?php
  2. $fruits = array("apple","banana","pear");
  3. $numbered = array("1","2","3");
  4. $cards = array_merge($fruits, $numbered);
  5. print_r($cards);
  6. //output
  7. //Array ([0] => apple [1] => banana [2] => pear [3] => 1 [4] => 2 [5] => 3)
  8. ?>

2. Append array

The array_merge_recursive() function is the same as array_merge(), it can merge two or more arrays together to form a combined array. The difference between the two is that when a key in an input array already exists in the result array, the function will take different processing methods. array_merge() will overwrite the previously existing key/value pairs and replace them with the key/value pairs in the current input array, while array_merge_recursive() will merge the two values ​​together to form a new array and use the original keys As the array name. There is also a form of array merging, which is to recursively append arrays. Its form is:

Php code

  1. array array_merge_recursive(array array1,array array2[…,array arrayN])

The program example is as follows:

Php code

  1. <?php
  2. $fruit1 = array("apple" => "red", "banana" => "yellow");
  3. $fruit2 = array("pear" => "yellow", "apple" => "green");
  4. $result = array_merge_recursive($fruit1, $fruit2);
  5. print_r($result);
  6. //output
  7. //Array ([apple] => Array ([0] => red [1] => green) [banana] => yellow [pear] => yellow)
  8. ?>

Now the key apple points to an array, which is an indexed array of two color values.

3. Connect the array

The array_combine() function will get a new array, which consists of a set of submitted keys and corresponding values. Its form is:

Java code

  1. array array_combine(array keys,array values)

Note that the two input arrays must be the same size and cannot be empty. The example is as follows

Php code

  1. <?php
  2. $name = array("apple", "banana", "orange");
  3. $color = array("red", "yellow", "orange");
  4. $fruit = array_combine($name, $color);
  5. print_r($fruit);
  6. //output
  7. //Array ([apple] => red [banana] => yellow [orange] => orange)
  8. ?>

4. Split the array array_slice()

The array_slice() function will return a part of the array, starting from the key offset and ending at the offset+length position. Its form:

Php code

  1. array array_slice (array array, int offset[,int length])

When the offset is positive, the split will start at the offset position from the beginning of the array; if the offset is negative, the split will start at the offset position from the end of the array. If the optional parameter length is omitted, the split will start at offset and continue to the last element of the array. If length is given and it is a positive number, it will end at offset+length from the beginning of the array. Conversely, if length is given and it is a negative number, it ends at the count(input_array)-|length| position from the beginning of the array. Consider an example:

Php code

  1. <?php
  2. $fruits = array("Apple", "Banana", "Orange", "Pear", "Grape", "Lemon", "Watermelon");
  3. $subset = array_slice($fruits, 3);
  4. print_r($subset);
  5. //output
  6. //Array ([0] => Pear [1] => Grape [2] => Lemon [3] => Watermelon)
  7. ?>

Then we use the next negative length:

Php code

  1. <?php
  2. $fruits = array("Apple", "Banana", "Orange", "Pear", "Grape", "Lemon", "Watermelon");
  3. $subset = array_slice($fruits, 2, -2);
  4. print_r($subset);
  5. //output
  6. //Array ([0] => Orange [1] => Pear [2] => Grape)
  7. ?>
Reference: https://cloud.tencent.com/developer/article/1053777 php array operations (review)-Cloud + Community-Tencent Cloud