Разделить элементы массива php

Разделить элементы массива php

  • PHP Tutorial
  • PHP | Introduction
  • PHP Full Form
  • How to set PHP development environment in windows ?
  • LAMP installation and important PHP configurations on Ubuntu
  • PHP | Coding Standards
  • PHP | Basic Syntax
  • PHP | Variables
  • PHP echo and print
  • PHP | Data Types
  • PHP | Strings
  • PHP | Constants
  • PHP | Magic Constants
  • PHP | Decision Making
  • PHP | Loops
  • PHP | Superglobals
  • PHP | Regular Expressions
  • PHP Examples
  • Describe PHP Include and Require
  • PHP | Basics of File Handling
  • PHP | fopen( ) (Function open file or URL)
  • PHP fread( ) Function
  • PHP fclose( ) Function
  • PHP | fwrite( ) Function
  • PHP | Uploading File
  • PHP Cookies
  • PHP | Sessions
  • PHP Filter and Filter Constant
  • Implementing callback in PHP
  • Exception Handling in PHP
  • PHP | Arrays
  • PHP array() Function
  • PHP | array_change_key_case() Function
  • PHP array_chunk() Function
  • PHP | array_column() Function
  • PHP array_combine() Function
  • PHP array_count_values() Function
  • PHP array_diff() function
  • PHP array_diff_assoc() Function
  • PHP array_diff_key() Function
  • PHP array_diff_uassoc() Function
  • PHP array_diff_ukey() Function
  • PHP array_fill() function
  • PHP array_fill_keys() Function
  • PHP Array Functions Complete Reference
  • PHP Date and Time
  • PHP | checkdate() Function
  • PHP | date_create(), date_format(), date_add() Functions
  • PHP | date_create_from_format() Function
  • PHP | date_date_set() Function
  • PHP | date_default_timezone_get() Function
  • PHP | date_default_timezone_set() Function
  • PHP | date_diff() Function
  • PHP | date_get_last_errors() Function
  • PHP | date_isodate_set() Function
  • PHP | date_modify() Function
  • PHP | date_offset_get() Function
  • PHP | date_parse_from_format() Function
  • PHP | date_parse() Function
  • PHP | date_sub() Function
  • PHP basename( ) Function
  • PHP chgrp( ) Function
  • PHP chmod( ) Function
  • PHP chown( ) Function
  • PHP copy( ) Function
  • PHP dirname( ) Function
  • PHP disk_free_space( ) Function
  • PHP disk_total_space( ) Function
  • PHP feof( ) Function
  • PHP fflush( ) Function
  • PHP | fgetc( ) Function
  • PHP | fgets( ) Function
  • PHP | fgetss( ) Function
  • PHP | file_exists( ) Function
  • PHP file_get_contents() Function
  • PHP Filesystem Functions Complete Reference
  • PHP | ftp_alloc() function
  • PHP | ftp_chdir() function
  • PHP | ftp_close() function
  • PHP | ftp_connect() function
  • PHP | ftp_delete() function
  • PHP | ftp_exec() function
  • PHP | ftp_get() function
  • PHP | ftp_put() function
  • PHP | ftp_get_option() function
  • PHP | ftp_login() function
  • PHP | ftp_mdtm() Function
  • PHP | ftp_mkdir() function
  • PHP | Ds\Map allocate() Function
  • PHP | Ds\Map apply() Function
  • PHP | Ds\Map capacity() Function
  • PHP | DS\Map clear() Function
  • PHP | Ds\Map __construct() Function
  • PHP Ds\Map copy() Function
  • PHP | Ds\Map count() Function
  • PHP | Ds\Map diff() Function
  • PHP | Ds\Map filter() Function
  • PHP | Ds\Map first() Function
  • PHP Ds\Map Functions Complete Reference
  • SplDoublyLinkedList in PHP
  • PHP SplDoublyLinkedList add() Function
  • PHP SplDoublyLinkedList bottom() Function
  • PHP SplDoublyLinkedList count() function
  • PHP SplFixedArray count() Function
  • PHP SplFixedArray current() Function
  • PHP SplFixedArray getSize() Function
  • PHP SplFixedArray key() Function
  • PHP SplObjectStorage addAll() Function
  • PHP SplObjectStorage attach() Function
  • PHP SplObjectStorage contains() Function
  • PHP SplObjectStorage count() Function
  • PHP SplQueue::__construct() Function
  • PHP SPL Data structures Complete Reference
  • PHP Tutorial
  • PHP | Introduction
  • PHP Full Form
  • How to set PHP development environment in windows ?
  • LAMP installation and important PHP configurations on Ubuntu
  • PHP | Coding Standards
  • PHP | Basic Syntax
  • PHP | Variables
  • PHP echo and print
  • PHP | Data Types
  • PHP | Strings
  • PHP | Constants
  • PHP | Magic Constants
  • PHP | Decision Making
  • PHP | Loops
  • PHP | Superglobals
  • PHP | Regular Expressions
  • PHP Examples
  • Describe PHP Include and Require
  • PHP | Basics of File Handling
  • PHP | fopen( ) (Function open file or URL)
  • PHP fread( ) Function
  • PHP fclose( ) Function
  • PHP | fwrite( ) Function
  • PHP | Uploading File
  • PHP Cookies
  • PHP | Sessions
  • PHP Filter and Filter Constant
  • Implementing callback in PHP
  • Exception Handling in PHP
  • PHP | Arrays
  • PHP array() Function
  • PHP | array_change_key_case() Function
  • PHP array_chunk() Function
  • PHP | array_column() Function
  • PHP array_combine() Function
  • PHP array_count_values() Function
  • PHP array_diff() function
  • PHP array_diff_assoc() Function
  • PHP array_diff_key() Function
  • PHP array_diff_uassoc() Function
  • PHP array_diff_ukey() Function
  • PHP array_fill() function
  • PHP array_fill_keys() Function
  • PHP Array Functions Complete Reference
  • PHP Date and Time
  • PHP | checkdate() Function
  • PHP | date_create(), date_format(), date_add() Functions
  • PHP | date_create_from_format() Function
  • PHP | date_date_set() Function
  • PHP | date_default_timezone_get() Function
  • PHP | date_default_timezone_set() Function
  • PHP | date_diff() Function
  • PHP | date_get_last_errors() Function
  • PHP | date_isodate_set() Function
  • PHP | date_modify() Function
  • PHP | date_offset_get() Function
  • PHP | date_parse_from_format() Function
  • PHP | date_parse() Function
  • PHP | date_sub() Function
  • PHP basename( ) Function
  • PHP chgrp( ) Function
  • PHP chmod( ) Function
  • PHP chown( ) Function
  • PHP copy( ) Function
  • PHP dirname( ) Function
  • PHP disk_free_space( ) Function
  • PHP disk_total_space( ) Function
  • PHP feof( ) Function
  • PHP fflush( ) Function
  • PHP | fgetc( ) Function
  • PHP | fgets( ) Function
  • PHP | fgetss( ) Function
  • PHP | file_exists( ) Function
  • PHP file_get_contents() Function
  • PHP Filesystem Functions Complete Reference
  • PHP | ftp_alloc() function
  • PHP | ftp_chdir() function
  • PHP | ftp_close() function
  • PHP | ftp_connect() function
  • PHP | ftp_delete() function
  • PHP | ftp_exec() function
  • PHP | ftp_get() function
  • PHP | ftp_put() function
  • PHP | ftp_get_option() function
  • PHP | ftp_login() function
  • PHP | ftp_mdtm() Function
  • PHP | ftp_mkdir() function
  • PHP | Ds\Map allocate() Function
  • PHP | Ds\Map apply() Function
  • PHP | Ds\Map capacity() Function
  • PHP | DS\Map clear() Function
  • PHP | Ds\Map __construct() Function
  • PHP Ds\Map copy() Function
  • PHP | Ds\Map count() Function
  • PHP | Ds\Map diff() Function
  • PHP | Ds\Map filter() Function
  • PHP | Ds\Map first() Function
  • PHP Ds\Map Functions Complete Reference
  • SplDoublyLinkedList in PHP
  • PHP SplDoublyLinkedList add() Function
  • PHP SplDoublyLinkedList bottom() Function
  • PHP SplDoublyLinkedList count() function
  • PHP SplFixedArray count() Function
  • PHP SplFixedArray current() Function
  • PHP SplFixedArray getSize() Function
  • PHP SplFixedArray key() Function
  • PHP SplObjectStorage addAll() Function
  • PHP SplObjectStorage attach() Function
  • PHP SplObjectStorage contains() Function
  • PHP SplObjectStorage count() Function
  • PHP SplQueue::__construct() Function
  • PHP SPL Data structures Complete Reference
Читайте также:  Jbutton java что это

Источник

array_chunk

Chunks an array into arrays with length elements. The last chunk may contain less than length elements.

Parameters

When set to true keys will be preserved. Default is false which will reindex the chunk numerically

Return Values

Returns a multidimensional numerically indexed array, starting with zero, with each dimension containing length elements.

Errors/Exceptions

If length is less than 1 , a ValueError will be thrown.

Changelog

Version Description
8.0.0 If length is less than 1 , a ValueError will be thrown now; previously, an error of level E_WARNING has been raised instead, and the function returned null .

Examples

Example #1 array_chunk() example

$input_array = array( ‘a’ , ‘b’ , ‘c’ , ‘d’ , ‘e’ );
print_r ( array_chunk ( $input_array , 2 ));
print_r ( array_chunk ( $input_array , 2 , true ));
?>

The above example will output:

Array ( [0] => Array ( [0] => a [1] => b ) [1] => Array ( [0] => c [1] => d ) [2] => Array ( [0] => e ) ) Array ( [0] => Array ( [0] => a [1] => b ) [1] => Array ( [2] => c [3] => d ) [2] => Array ( [4] => e ) )

See Also

User Contributed Notes 20 notes

Tried to use an example below (#56022) for array_chunk_fixed that would «partition» or divide an array into a desired number of split lists — a useful procedure for «chunking» up objects or text items into columns, or partitioning any type of data resource. However, there seems to be a flaw with array_chunk_fixed — for instance, try it with a nine item list and with four partitions. It results in 3 entries with 3 items, then a blank array.

So, here is the output of my own dabbling on the matter:

function partition ( $list , $p ) $listlen = count ( $list );
$partlen = floor ( $listlen / $p );
$partrem = $listlen % $p ;
$partition = array();
$mark = 0 ;
for ( $px = 0 ; $px < $p ; $px ++) $incr = ( $px < $partrem ) ? $partlen + 1 : $partlen ;
$partition [ $px ] = array_slice ( $list , $mark , $incr );
$mark += $incr ;
>
return $partition ;
>

$citylist = array( «Black Canyon City» , «Chandler» , «Flagstaff» , «Gilbert» , «Glendale» , «Globe» , «Mesa» , «Miami» ,
«Phoenix» , «Peoria» , «Prescott» , «Scottsdale» , «Sun City» , «Surprise» , «Tempe» , «Tucson» , «Wickenburg» );
print_r ( partition ( $citylist , 3 ) );

?>

Array
(
[0] => Array
(
[0] => Black Canyon City
[1] => Chandler
[2] => Flagstaff
[3] => Gilbert
[4] => Glendale
[5] => Globe
)

[1] => Array
(
[0] => Mesa
[1] => Miami
[2] => Phoenix
[3] => Peoria
[4] => Prescott
[5] => Scottsdale
) [2] => Array
(
[0] => Sun City
[1] => Surprise
[2] => Tempe
[3] => Tucson
[4] => Wickenburg
)

Источник

PHP array_chunk() function

The PHP array_chunk() method is a sophisticated array manipulation tool that divides an array into smaller, more manageable pieces. Whether you’re dealing with a huge dataset or just want to arrange your data, this function makes it simple to split an array into different sections, giving you greater flexibility over how you interact with it. Whether you want to boost speed or simply make your code more legible and manageable, this method provides a simple yet effective array manipulation solution.

What is the syntax of the array_chunk function in PHP?

array array_chunk ( array $input , int $size [, bool $preserve_keys = false ] )
Parameter Details
array Required. Array to split into chunks
size Required (integer). Set the size of each chunk
preserve_key This is an Optional parameter.
Preserves the keys when set to true
Reindexes the chunk numerically when set to false

Array_chunk function in PHP

The array_chunk() function returns an multidimensional array, containing the chunks as arrays.

Examples of the array_chunk function

$fruits = array("Apple", "Banana", "Cherry", "date", "Elderberry", "Fig", "Grape", "Honeydew"); $chunks = array_chunk($fruits, 2); print_r($chunks);

In this example, we’ll make a variety of fruits. The array_chunk() method is then used to divide it into 2 parts. The resultant array will be a multidimensional array with four subarrays, the first of which will have two items, the second of which will have two elements, and so on.

$numbers = array(1, 2, 3, 4, 5, 6, 7, 8); $chunks = array_chunk($numbers, 3, true); print_r($chunks);

In this example, we’ll make a number array. Then we divide it into three chunks with the array_chunk() method and set the third option to true to keep the keys. The resultant array will be a multidimensional array with three subarrays, the first of which will have three items, the second of which will have three elements, and so on, with the keys intact.

$employee = array("Name" => "John Doe", "Age" => 30, "Salary" => 5000, "Designation" => "Developer"); $chunks = array_chunk($employee, 2, true); print_r($chunks);

In this example, we’ll make an associative array containing employee information. The array_chunk() method is then used to divide it into chunks of two, and the third parameter is set to true to preserve the keys. The resultant array will be a multidimensional array with two subarrays; the first will have two items, the second will have two elements, and the keys will be kept.

It’s vital to notice that the array_chunk() method returns a new array containing the updated chunks rather than modifying the old array.

It’s also worth noting that if the total number of elements in the array is greater than the chunk size, the last chunk will have less elements than the others.

For more information see the official reference.

Источник

Оцените статью