当前位置:网站首页>Introduction notes to PHP zero Foundation (13): array related functions

Introduction notes to PHP zero Foundation (13): array related functions

2022-04-23 16:14:00 Peng Shiyu psy

Array related functions

Custom array print function , Easy to view


function print_array($array){
    foreach($array as $key => $value){
        echo "$key => $value\n";
    }
}


     
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.

Sorting function

according to ASCII Sort code

sort Yes array By itself (value) Ascending sort .( Subscript rearrangement )

sort(array &$array, int $flags = SORT_REGULAR): bool


// eg:
$arr = ['Tom', 'Jack', 'Steve'];

sort($arr);

print_array($arr);
// 0 => Jack
// 1 => Steve
// 2 => Tom


     
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.

rsort Yes array By itself (value) null .

rsort(array &$array, int $flags = SORT_REGULAR): bool


// eg:
$arr = ['Tom', 'Jack', 'Steve'];

rsort($arr);

print_array($arr);
// 0 => Tom
// 1 => Steve
// 2 => Jack

     
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.

asort Yes array Sort itself in ascending order ( Subscript reserved )

asort(array &$array, int $flags = SORT_REGULAR): bool


// eg:
$arr = ['Tom', 'Jack', 'Steve'];

asort($arr);

print_array($arr);
// 1 => Jack
// 2 => Steve
// 0 => Tom

     
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.

arsort Yes array Sort itself in descending order

arsort(array &$array, int $flags = SORT_REGULAR): bool


// eg:
$arr = ['Tom', 'Jack', 'Steve'];

arsort($arr);

print_array($arr);
// 0 => Tom
// 2 => Steve
// 1 => Jack

     
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.

ksort Yes array Press the button itself (key) Ascending sort .

ksort(array &$array, int $flags = SORT_REGULAR): bool


// eg:
$arr = ['Tom', 'Jack', 'Steve'];

ksort($arr);

print_array($arr);
// 0 => Tom
// 1 => Jack
// 2 => Steve

     
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.

krsort Yes array Press the key itself (key) null .

krsort(array &$array, int $flags = SORT_REGULAR): bool


// eg:
$arr = ['Tom', 'Jack', 'Steve'];

krsort($arr);

print_array($arr);
// 2 => Steve
// 1 => Jack
// 0 => Tom

     
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.

shuffle Random disruption

shuffle(array &$array): bool


// eg:
$arr = ['Tom', 'Jack', 'Steve'];

shuffle($arr);

print_array($arr);
// 0 => Jack
// 1 => Steve
// 2 => Tom

     
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.

Pointer function

reset take array The internal pointer of the array returns to the first cell and returns the value of the first array cell .

reset(array|object &$array): mixed

     
  • 1.

end take array The internal pointer to the last cell and returns its value .

end(array|object &$array): mixed

     
  • 1.

next Move the internal pointer in the array one bit forward

next(array|object &$array): mixed

     
  • 1.

prev Returns the internal pointer of an array to one bit

prev(array|object &$array): mixed

     
  • 1.

current Returns the current value in the array

current(array|object $array): mixed

     
  • 1.

key Returns the key name of the current cell in the array .

key(array|object $array): int|string|null

     
  • 1.

Be careful :next and prev Move the pointer , May move out of the array , Only through end perhaps reset Reset pointer

Example


$arr = [1, 3, 5, 7, 9];

echo next($arr);
echo next($arr);
echo prev($arr);
echo current($arr);
echo key($arr);
echo reset($arr);
echo end($arr);
// 3533119

     
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.

Other functions

count Statistical array 、Countable The number of all elements in the object

count(Countable|array $value, int $mode = COUNT_NORMAL): int

// eg:
$arr = [1, 2, 3, 4, 5];

echo count($arr);
// 5

     
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.

array_push take array As a stack , And push the incoming variable into array At the end of .

array_push(array &$array, mixed $value1, mixed $... = ?): int

//  The same effect as the following :
$array[] = $var;

     
  • 1.
  • 2.
  • 3.
  • 4.

array_pop Pop up and return array The value of the last element

array_pop(array &$array): mixed

     
  • 1.

array_shift take array The first cell of is moved out and returned as a result

array_shift(array &$array): mixed

     
  • 1.

array_unshift Insert one or more cells at the beginning of an array

array_unshift(array &$array, mixed ...$values): int

     
  • 1.

data structure

  • Stack Pressing stack ,FILO First in, then out
  • queue line up ,FIFO fifo

Implementation stack :

  • front array_unshift/array_shift
  • Back array_push/array_pop
<?php $arr = []; //  Push  array_push($arr, 1); array_push($arr, 2); array_push($arr, 3); //  Out of the stack  echo array_pop($arr), array_pop($arr), array_pop($arr); // 321 
     
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.

Implementation queue :

  • Last in first out array_push/array_shift
  • Forward and out array_unshift/array_pop
<?php $arr = []; //  The team  array_push($arr, 1); array_push($arr, 2); array_push($arr, 3); //  Out of the team  echo array_shift($arr), array_shift($arr), array_shift($arr); // 123 
     
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.

array_reverse Returns a new array whose cells are in reverse order

array_reverse(array $array, bool $preserve_keys = false): array

// eg
$arr = [1, 2, 3, 4, 5];

print_array(array_reverse($arr));
// 0 => 5
// 1 => 4
// 2 => 3
// 3 => 2
// 4 => 1

     
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.

in_array Check if there is a value in the array

in_array(mixed $needle, array $haystack, bool $strict = false): bool

// eg
$arr = [1, 2, 3, 4, 5];

var_dump(in_array(5, $arr));
// bool(true)


     
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.

array_keys Returns some or all key names in the array

array_keys(array $array): array

array_keys(array $array, mixed $search_value, bool $strict = false): array


// eg
$arr = ['Tom', 'Jack', 'Steve'];

echo json_encode(array_keys($arr));
// [0,1,2]

     
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.

array_values return input All values in the array and numerically index them .

array_values(array $array): array

// eg
$arr = ['Tom', 'Jack', 'Steve'];

echo json_encode(array_values($arr));
// ["Tom","Jack","Steve"]

     
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.

版权声明
本文为[Peng Shiyu psy]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231521404553.html