PHP array operations

高洛峰
Release: 2016-10-21 09:59:56
Original
972 people have browsed it

Method 1:

<?PHP
$array = array(1,2,4,6,8);
echo end($array);
?>
Copy after login

Output

//输出8
Copy after login
Copy after login

Method 2:

<?PHP
$array = array(1,2,4,6,8);
echo array_pop($array);
?>
Copy after login

Output

//输出8
Copy after login
Copy after login


Method 3:

<?PHP
$array = array(1,2,4,6,8);
$k = array_slice($array,-1,1);
print_r($k);  //结果是一维数组
?>
Copy after login

Output

Array ( [0] => 8 )
Copy after login



The second method has a drawback. The Array_pop() function will "take out" the last number of the original data, which is equivalent to cutting. It turns out There will no longer be a last value in the data. Re re:

<?PHP
$array = array(1,2,4,6,8);
array_pop($array);
print_r($array);
?>
Copy after login
E

Output:

Array ( [0] => 1 [1] => 2 [2] => 4 [3] => 6 )
Copy after login



is a built -in function in PHP. In fact, we can also obtain the PHP array through some of the methods we write The last element in

<?php
$array = array(1,2,4,6,8);
$last = count($array);
echo $array[$last-1];
?>
Copy after login
🎜🎜🎜🎜Of course, if the built-in method can be implemented, we try to use PHP’s built-in function, because the built-in method is the most efficient in terms of efficiency and time, but then develop During the process, if we don't know the existence of this method or don't remember this method, we can also write the function method we want based on our own logic. 🎜🎜🎜🎜
Related labels:
php
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!