Jump to content

user defined array_chunk()


lipun4u

Recommended Posts

I had written a user defined array_chunk() in php....

<?php
header("Content_Type: Text/plain");

function user_array_chunk($arr, $size) {
	$arrs = array();
	$k = -1;
	$i = 0;
	foreach($arr as $ar1) {
		if(($i % $size) == 0) {
			$k ++;
			$j = 0;
			$arrs[k] = array();
		}
		$arrs[k][j++]= $ar1;
	}
	return $arrs;
}
$input_array = array('a', 'b', 'c', 'd', 'e');
print_r(user_array_chunk($input_array, 2));
?>

 

Why it shows errors ??

Link to comment
https://forums.phpfreaks.com/topic/176464-user-defined-array_chunk/
Share on other sites

just killing time...

 

thanx for ur suggestion..

 

the correct code is

<?php
header("Content_Type: Text/plain");

function user_array_chunk($arr, $size) {
	$arrs = array();
	$k = -1;
	$i = 0;
	foreach($arr as $ar1) {
		if(($i % $size) == 0) {
			$k ++;
			$j = 0;
			$arrs[$k] = array();
		}
		$arrs[$k][$j++]= $ar1;
		$i ++;
	}
	return $arrs;
}
$input_array = array('a', 'b', 'c', 'd', 'e');
print_r(user_array_chunk($input_array, 2));
?>	

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.