Php

PHP: Split an array into chunks

The array_chunk() function is used to split an array into arrays with size elements. The last chunk may contain less than size elements.

Bạn đang xem: Php

Version:

(PHP 4 & above)

Syntax:

array_chunk(input_array, size, preserve_keys)Parameters:


NameDescriptionRequired /Optional Type
input_arraySpecifies the array khổng lồ split.RequiredArray
sizeThe size of each chunk (i.e. The number of elements)RequiredInteger
preserve_keysIf we setpreserve_keysas TRUE, array_chunk function preserves the original array keys. The mặc định is FALSE which will reindex the chunk numerically.OptionalBoolean

Return value:

A multidimensional numerically indexed array.

Xem thêm: Abstract Class Vs Interface :: Code In Php, Class Abstraction

Value Type: Array

Example - 1:

Output:

Array ( <0> => Array ( <0> => name1 <1> => name2 ) <1> => Array ( <0> => name3 <1> => name4) <2> => Array (<0> => name5 )) Pictorial Presentation:

*

View the example in the browser

Example - 2:

In the following example, we setpreserve_keysas TRUE, therefore, array_chunk function preserves the original array keys.

Output:

Array ( <0> => Array ( <0> => name1 <1> => name2 ) <1> => Array ( <2> => name3 <3> => name4 ) <2> => Array ( <4> => name5 ) ) View the example in the browser

Practice here online :

See also

PHP Function Reference

Previous: array_change_ key_caseNext: array_combine



PHP: Tips of the Day

mysql_fetch_array()/mysql_fetch_assoc()/mysql_fetch_row()/mysql_num_rows etc� expects parameter 1 khổng lồ be resource

A query may fail for various reasons in which case both the mysql_* và the mysqli extension will return false from their respective query functions/methods. You need to chạy thử for that error condition and handle it accordingly.

mysql_* extension:

NOTEThemysql_ functions are deprecatedand have been removed in php version 7.

Check$resultbefore passing it tomysql_fetch_array. You"ll find that it"sfalsebecause the query failed. See themysql_querydocumentation for possible return values và suggestions for how to giảm giá khuyến mãi with them.

$username = mysql_real_escape_string($_POST<"username">);$password = $_POST<"password">;$result = mysql_query("SELECT * FROM Users WHERE UserName lượt thích "$username"");if($result === FALSE) die(mysql_error()); // TODO: better error handlingwhile($row = mysql_fetch_array($result)) echo $row<"FirstName">;mysqli extension

procedural style:

$username = mysqli_real_escape_string($mysqli, $_POST<"username">);$result = mysqli_query($mysqli, "SELECT * FROM Users WHERE UserName lượt thích "$username"");// mysqli_query returns false if something went wrong with the queryif($result === FALSE) yourErrorHandler(mysqli_error($mysqli));else { // as of php 5.4 mysqli_result implements Traversable, so you can use it with foreach foreach( $result as $row ) { ...oo-style:

$username = $mysqli->escape_string($_POST<"username">);$result = $mysqli->query("SELECT * FROM Users WHERE UserName lượt thích "$username"");if($result === FALSE) yourErrorHandler($mysqli->error); // or $mysqli->error_listelse { // as of php 5.4 mysqli_result implements Traversable, so you can use it with foreach foreach( $result as $row ) { ...using a prepared statement:

$stmt = $mysqli->prepare("SELECT * FROM Users WHERE UserName lượt thích ?");if ( !$stmt ) yourErrorHandler($mysqli->error); // or $mysqli->error_listelse if ( !$stmt->bind_param("s", $_POST<"username">) ) yourErrorHandler($stmt->error); // or $stmt->error_listelse if ( !$stmt->execute() ) yourErrorHandler($stmt->error); // or $stmt->error_listelse { $result = $stmt->get_result(); // as of php 5.4 mysqli_result implements Traversable, so you can use it with foreach foreach( $result as $row ) { ...These examples only illustrate what should be done (error handling), not how to vị it. Production code shouldn"t use or die when outputting HTML, else it will (at the very least) generate invalid HTML. Also, database error messages shouldn"t be displayed lớn non-admin users, as it discloses too much information.