php array only take the first two elements of the method: 1, use the array_splice () function, syntax "array_splice($arr,0,2)"; 2, use the array_slice () function, syntax "array_slice($arr,0,2)".
php array method to take only the first two elements
Method 1: Use array_splice() function
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
<?php
header( "Content-type:text/html;charset=utf-8" );
$arr = array (10,12,20,25,24);
echo "Original array:";
var_dump( $arr );
echo "Intercepted array fragments:";
$result = array_splice ( $arr ,0); //Truncate all the elements of the array starting from subscript 0
var_dump( $result );
$arr = array (10,12,20,25,24);
$result = array_splice ( $arr ,0,2);//Intercept the two elements starting from subscript 0
var_dump( $result );
?>
|
Method 2: Use array_slice() function
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
<?php
header( "Content-type:text/html;charset=utf-8" );
$arr = array (12,20,25,24,29, "2" );
echo "Original array:";
var_dump( $arr );
echo "Intercepted array fragments:";
$result = array_slice ( $arr ,0); //Truncate all the elements of the array starting from subscript 2
var_dump( $result );
$result = array_slice ( $arr ,0,2); //Intercept the two elements starting from subscript 0
var_dump( $result );
?>
|
Related articles
String is an important data type in PHP, which how to compare strings is also very common in our development work, there are many kinds of comparison methods to compare strings, here we will introduce the more commonly used several comparison methods.
Difference: 1. Jump is the current URL request success, re-request a new URL; while redirect is the current URL is invalid, was redirected to a new URL. 2. In the jump, the browser will record the current URL and the new URL to the history; while redirect
php merge different array elements method: 1, create a PHP sample file; 2, by "array_keys(array_flip($a) + array_flip($b) + array_flip($c));" way to multiple arrays can be merged and de-weighted.
The solution for the php exec function not taking effect: 1. Open the /etc/php.ini file, delete the exec, then save and restart php-fpm; 2. Change the value of "safe_mode" to off.
The difference between ado and php: 1, ADO is a Microsoft technology, is a programming interface to access data in the database, while PHP is a general open source scripting language;
How to set the local time in php: 1. Use "date_default_timezone_set()" in the header to set the default time zone; 2. Set "date.timezone" in php.ini " in php.ini to set the value of "date.timezone" to PRC.
How to set the error message in php.ini: 1. Find and open the php.ini file; 2. Find "display_errors = On" and change it to "display_errors = off". This will work.
The two most common variable types in PHP are array and string, in the process of development, it is inevitable to encounter how to convert string to array, or array to string.
The difference between PHP synchronous and asynchronous: 1. Traditional synchronous programming is a request-response model
In php echo is used to output one or more strings. The syntax is "echo(strings)" and the argument strings represents one or more strings to be sent to the output.