[daily function] daily function (August 12, 2021)

Time:2021-9-24

A dream Millennium Series

Today’s functionarray_searcharray_shiftarray_slicearray_splicearray_sum

array_search1

Search the specified value in the array. If it is found, the first key name is returned. If it is not found, false is returned

  1. explain
array_search(mixed $needle, array $array, bool $strict = false) : mixed
  1. parameter

needleThe value to be searched is case sensitive if it is a string

arraySearch array

strictStrictly compare the same

  1. Return value

Returns the searched key name. If it is not found, false is returned

  1. example
$array = [
    'name' => 'swk',
    'age' => 22,
    'sex' => 1
];

$res = array_search('swk', $array);

print_r($res);

The above example will be output

name
  1. Particular attention

The returned key name is found, and false is returned if not found

array_shift2

Removes the element at the beginning of the array from the array

  1. explain
array_shift(array $array) : array
  1. parameter

arrayArray of operations required

  1. Return value

Returns the element value of the removed array. If the array is empty, null is returned

  1. example
$array = [
    'name' => 'swk',
    'age' => 22,
    'sex' => 1
];

array_shift($array);

print_r($array);

Above example output

Array
(
    [age] => 22
    [sex] => 1
)
  1. Special note

After the element is removed, the index key is re indexed

array_slice3

Intercepts a segment in the array

  1. explain
array_slice(array $array, int $offset, int $length = null, bool $preserve_key = false) : array
  1. parameter

arrayOriginal array to be intercepted

offsetThe position where the interception starts. If it is positive, it starts from the offset of the array, and if it is negative, it starts from the end distance of the array

preserve_keyIf the array is an indexed array, do you want to keep the key name

  1. Return value

Returns the intercepted array

  1. example
$array = [1, 2, 3, 4, 5];
$newArray = array_slice($array, 3, 2);

print_r($newArray);

Above example output

Array
(
    [0] => 4
    [1] => 5
)
  1. Special note

Remember that the intercepted value of the array can only be intercepted from front to back. Offset is used as the calculation start position and length is used as the calculation end position. If the start position is greater than the array length, an empty array will be returned. If the end position is before the start, an empty array will also be returned

array_splice4

Replace part of an array

  1. explain
array_splice(array &$array, int $offset, int $length = count($input), mixed $replacement  []) : array
  1. parameter

arrayArray of operations required

offsetReplace the start position. If it is a positive number, it starts from the offset position of the array. If it is a negative number, it starts from the end of the distance

lengthThe position where the replacement ends. If it is a positive number, it is the number of arrays to be replaced. If it is a negative number, it stops at the end of the number

replacementThe removed part of the replaced variable will be replaced by this variable. If it is empty, it will not be replaced, but only removed

  1. Return value

Returns the removed part

  1. example
$array = [1, 2, 3, 4, 5];
$replacement = [7, 8, 9];

array_splice($array, 3, 2, $replacement);

print_r($array);

Above example output

Array
(
    [0] => 
    [1] => 2
    [2] => 3
    [3] => 7
    [4] => 8
    [5] => 9
)
  1. Special note

Pay attention to the calculation start position and settlement position

array_sum5

Sums all the values of the array

  1. explain
array_sum(array $array) : number
  1. parameter

arraySum array

  1. Return value

Returns the sum of an array

  1. example
$array = [1, 2, 3, 4, 5];

$res = array_sum($array);

print_r($res);

Above example output

15
  1. Special note

If there are objects or non numeric values in the array, they are calculated as 0, and the sum of empty arrays is 0