EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login

PHP Split Array

Home » Software Development » Software Development Tutorials » PHP Tutorial » PHP Split Array

PHP Split Array

Introduction to PHP Split Array

Dealing with the arrays and the array related operations are very common in the PHP Programming Language, a split array is one of them. PHP itself has various built-in functions to handle this. A developer or the coder can do the same by writing their own custom code. Split is all about converting a single array into multiple arrays. An array can be split into the number of chunks. A built-in function array_chunk() can be used to split the array into multiple arrays with a defined number of elements.

Syntax:

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

1. array_chunk()

array_chunk(array, size, preserve_key)

  • array_chunk() is the function itself.
  • The array and size are the required parameters.
  • preserve_key takes the boolean value.

2. array_slice()

array_slice(array, start, length, preserve)

  • array_slice() is the function itself.
  • The array and the start are the required parameters.
  • Length and the preserve are the optional parameters. The start parameter shows from which position the array needs to slice and the length is to what length.

The output of array_chunk(directly assigned to the multiple arrays):

list($array1, $array2,.....) = array_chunk($array, length);

$array1, $array2…. are the array in which the array element will be assigned after the split from the $array. A developer or the code should be careful about the number of elements and the size of the array in which the elements need to be assigned.

How PHP Split Array work?

Given below shows how PHP Split Array works:

1. Use of array_chunk()

Before playing with the split functionality with the array there should be an array with some elements on that. Then we can apply the array_chunk() function to perform the array split related operations. This function is useful when we are required to split an array into the number of defined elements. Using array_chunk() function, the output can be stored into a single array and the other hand the output can be stored on the multiple arrays as well.

2. Use of array_slice()

It is another way of splitting an array, in this we can get the number of specific elements from an array.

3. Use of str_split()

A string can split into the array by using the str_split() function. This function can change each character of that string into an array.

Example:

Code:

$string = "Hello India";
print_r(str_split($string));

Examples of PHP Split Array

Given below are the examples mentioned:

Example #1

Split an array in the chunk of 2 elements and print the first segment of the new array.

Popular Course in this category
PHP Training (5 Courses, 3 Project)5 Online Courses | 3 Hands-on Project | 28+ Hours | Verifiable Certificate of Completion | Lifetime Access
4.5 (5,731 ratings)
Course Price

View Course

Related Courses
Java Servlet Training (6 Courses, 12 Projects)All in One Software Development Bundle (600+ Courses, 50+ projects)

Code:

<?php
$array = array('value -1', 'value 2', 'value 3', 'value 4', 'value 5','value -6','value -7');
$newArrays = array_chunk($array,2); // apply array chunk
echo "<pre>";
print_r($newArrays[0]); // print the first segment (position) array after splitting that array.
?>

Output:

PHP Split Array 1

Example #2

Let’s try to get to achieve the same using the array_slice() as Ex1.

Code:

<?php
$array = array('value -1', 'value 2', 'value 3', 'value 4', 'value 5','value -6','value -7');
$newArrays = array_slice($array,0,2); // apply slicing from 0 position with the length of 2
echo "<pre>";
print_r($newArrays);
?>

We can see the same output here as we see in example 1.

Output:

PHP Split Array 2

Example #3

Let’s try to split the array and assigned to predefined array.

Code:

<?php
$array = array('value -1', 'value 2', 'value 3', 'value 4');
echo "<pre>";
print_r($array); // print the first segment (position) array after splitting that array.
list($array1, $array2) = array_chunk($array, 2);
print_r($array1);
print_r($array2);
?>

Output:

 the array and assigned to predefined

In the output area we can see the three arrays. First one is the actual array, 2nd and the 3rd array is the part of the actual array after splitting.

Code:

list($array1, $array2) = array_chunk($array, 2);

In other words after the split, both arrays will be assigned automatically to $array1 and $array2 respectively.

Example #4

Use of array_chunk() with the multi-dimensional array.

Code:

<?php
$employees = array(
array("id" => 1,
"name" => "Alex Hales",
"dob" => "20 - 02 - 1990" ),
array("id" => 2,
"name" => "SachineWaghe",
"dob" => "20 - 02 - 1991" ),
array("id" => 3,
"name" => "Babita Sharma",
"dob" => "20 - 02 - 1992" ),
array("id" => 4,
"name" => "DeepikaChoubey",
"dob" => "20 - 02 - 1992" )
);
echo "<pre>";
print_r($employees); // actual array
$employeesArra = array_chunk($employees, 2);  // array after split
print_r($employeesArra);
?>

Output:

multi-dimensional

multi-dimensional

PHP Split Array 6

Conclusion

There are various ways we can process the split array. While using the array_chunk() with the dynamic array assignment the developer should be careful enough, because sometimes the array and the size can break the system functionality. The array_chunk() function can be used with the single array and the associate array as well. This function can be used on all types of array.

Recommended Articles

This is a guide to PHP Split Array. Here we discuss the introduction to PHP split array along with the working and programming examples. You may also have a look at the following articles to learn more –

  1. PHP strtok()
  2. PHP levenshtein()
  3. PHP parse_str()
  4. PHP is_null()

All in One Software Development Bundle (600+ Courses, 50+ projects)

600+ Online Courses

50+ projects

3000+ Hours

Verifiable Certificates

Lifetime Access

Learn More

0 Shares
Share
Tweet
Share
Primary Sidebar
PHP Tutorial
  • Array
    • What is PHP Array
    • Arrays in PHP
    • 2D Arrays in PHP
    • Associative Array in PHP 
    • Multidimensional Array in PHP
    • Indexed Array in PHP
    • PHP Array Functions
    • PHP unset Array
    • PHP Append Array
    • PHP Array Search
    • PHP Split Array
    • PHP array_push()
    • PHP array_pop()
  • PHP Basic
    • Introduction To PHP
    • What is PHP
    • PHP Keywords
    • Advantages of PHP
    • Career In PHP
    • Comments in PHP
    • PHP Commands
    • PHP Frameworks
    • PHP Compiler
    • Variables in PHP
    • PHP Superglobal Variables
    • PHP Versions
    • Object in PHP
    • What is Drupal
    • Top PHP Frameworks
    • WebStorm IDE
    • What is phpMyAdmin?
    • PhpStorm
    • Install phpMyAdmin
    • Phalcon Model
  • Data Types
    • PHP Data Types
    • PHP Integer
    • PHP Booleans
  • Operators
    • PHP Operators
    • Arithmetic Operators in PHP
    • Comparison Operators in PHP
    • Logical Operators in PHP
    • Bitwise Operators in PHP
    • Ternary Operator in PHP
    • PHP String Operators
  • Control Statements
    • Control Statement in PHP
    • PHP if Statement
    • if else Statement in PHP
    • elseif in PHP
    • PHP Switch Statement
    • Continue in PHP
    • Break in PHP
  • Loops
    • PHP Loops
    • For Loop in PHP
    • PHP Do While Loop
    • PHP While Loop
    • While Loop in PHP
    • Foreach Loop in PHP
  • Constructor
    • Constructor in PHP
    • Destructor in PHP
  • State Management
    • Cookie in PHP
    • Sessions in PHP
  • Functions
    • Functions in PHP
    • PHP Math Functions
    • PHP Recursive Function
    • PHP String Functions
    • Hashing Function in PHP
    • Date Function in PHP
    • PHP Anonymous Function
    • Calendar in PHP
    • PHP Call Function
    • PHP Pass by Reference
    • PHP ucfirst()
    • PHP ucwords()
    • trim() in PHP
    • isset() Function in PHP
    • PHP replace
    • PHP fpm
    • PHP strpos
    • preg_match in PHP
    • PHP preg_replace()
    • PHP ob_start()
    • PHP Reflection
    • PHP Split String
    • PHP URL
    • PHP preg_match_all
    • PHP strtoupper()
    • PHP preg_split()
    • PHP substr_replace()
    • PHP setlocale()
    • PHP substr_count()
    • PHP Serialize
    • PHP strlen()
    • PHP async
    • PHP Date Time Functions
    • PHP timezone
    • PHP Data Object
    • print_r() in PHP
    • PHP header()
    • PHP strip_tags()
    • PHP chop()
    • PHP MD5()
    • PHP unset()
    • PHP crypt()
    • PHP wordwrap()
    • PHP is_null()
    • PHP strtok()
    • PHP bin2hex()
    • PHP parse_str()
    • PHP levenshtein()
    • PHP addslashes()
    • PHP strtotime
    • PHP sha1()
    • PHP explode()
    • PHP sscanf()
    • PHP require_once
    • PHP Zip Files
    • PHP $_SERVER
    • PHP $_POST
    • PHP Include and Require
    • PHP POST Method
  • Advanced
    • Overloading in PHP
    • Overriding in PHP
    • Method Overloading in PHP
    • Inheritance in PHP
    • Multiple Inheritance in PHP
    • PHP Interface
    • Encapsulation in PHP
    • PHP Constants
    • PHP Magic Constants
    • PHP Regular Expressions
    • PHP GET Method
    • PHP Annotations
    • PHP Encryption
    • PHP file Functions
    • PHP readfile
    • PHP?Write File
    • PHP Append File
    • PHP Type Hinting
    • PHP Filters
    • PHP Float
    • PHP Form
    • PHP Form Validation
    • Sorting in PHP
    • PHP usort()
    • PHP Stack Trace
    • PHP Stack Overflow
    • PHP Pagination
    • PHP implode
    • Polymorphism in PHP
    • Abstract Class in PHP
    • PHP Final Class
    • PHP Custom Exception
    • error_reporting() in PHP
    • PHP Log Errors
    • Access Modifiers in PHP
    • PHP Change Date Format
    • Static Method in PHP
    • PHP File Handling
    • PHP Output Buffering
    • Get IP Address in PHP
    • Upload a File in PHP
    • String in PHP
    • Public Function in PHP
    • Private in PHP
    • Protected in PHP
    • basename in PHP
    • Validation in PHP
    • PHP mail()
    • PHP Email Form
    • PHP Directory
    • PHP Create Session
    • PHP include_once
    • PHP json_decode
    • PHP XMLWriter
    • PHP XML Reader
    • PHP XML Parser
    • PHP XML into Array
    • Phalcon Framework
  • Programs
    • Patterns in PHP
    • Star Patterns in PHP
    • Swapping in PHP
    • Fibonacci Series PHP
    • Factorial in PHP
    • Reverse String in PHP
    • Square Root in PHP
    • Random Number Generator in PHP
    • Palindrome in PHP
    • Prime Numbers in PHP
    • Armstrong Number in PHP
    • Socket Programming in PHP
    • Login Page in PHP
    • PHP Login Template
    • PHP Object to String
  • Database
    • PHP Database Connection
    • How to Connect Database to PHP
  • Interview Questions
    • PHP Interview Questions
    • PHP OOP Interview Questions
    • CakePHP Interview Questions
    • Core PHP Interview Questions

Related Courses

PHP Training Course

Java Servlet Training

Software Development Course Training

Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

© 2020 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you
Book Your One Instructor : One Learner Free Class

Let’s Get Started

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

Special Offer - PHP Training Course Learn More