• 11
name

A PHP Error was encountered

Severity: Notice

Message: Undefined index: userid

Filename: views/question.php

Line Number: 191

Backtrace:

File: /home/prodcxja/public_html/questions/application/views/question.php
Line: 191
Function: _error_handler

File: /home/prodcxja/public_html/questions/application/controllers/Questions.php
Line: 433
Function: view

File: /home/prodcxja/public_html/questions/index.php
Line: 315
Function: require_once

name Punditsdkoslkdosdkoskdo

How to split/divide an array into 2 using php?

Help me to split or divide an array into 2 different arrays. Here is my single array

$array = array("1","2","3","4","5","6");

I want the above array into two array like below

$array1 = array("1","2","3");

$array2 = array("4","5","6");
php

Use array_chunk:

$pieces = array_chunk($array, ceil(count($array) / 2));

If you want them in separate variables (instead of a multi-dimensional array), use list:

list($array1, $array2) = array_chunk($array, ceil(count($array) / 2));
  • 80
Reply Report

Warm tip !!!

This article is reproduced from Stack Exchange / Stack Overflow, please click

Trending Tags

Related Questions