Array functions

What is an array?

       Arrays are special variables that store multiple values ​​in a single variable.

1) count(): used to get the length of the array.

<?php
    $cars=array("Volvo","BMW","Toyota");
    echo count($cars);
?>

2) array_change_key_case(): Convert all keys of the array to uppercase letters.

<?php
    $age=array("Bill"=>"60","Steve"=>"56","Mark"=>"31");
    print_r(array_change_key_case($age,CASE_UPPER));
?>

3) array_chunk(): Split the array into an array with two elements.

<?php
    $cars=array("Volvo","BMW","Toyota","Honda","Mercedes","Opel");
    print_r(array_chunk($cars,2));
?>

4) array_column(): Returns the value of a single column in the array.

<?php
// 表示由数据库返回的可能记录集的数组
$a = array(
  array(
    'id' => 5698,
    'first_name' => 'Bill',
    'last_name' => 'Gates',
  ),
  array(
    'id' => 4767,
    'first_name' => 'Steve',
    'last_name' => 'Jobs',
  ),
  array(
    'id' => 3809,
    'first_name' => 'Mark',
    'last_name' => 'Zuckerberg',
  )
);

$last_names = array_column($a, 'last_name');
print_r($last_names);
?>

        Output:

Array
(
  [0] => Gates
  [1] => Jobs
  [2] => Zuckerberg
)

 

Guess you like

Origin blog.csdn.net/weixin_45849851/article/details/103023911