lipun4u Posted October 4, 2009 Share Posted October 4, 2009 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 More sharing options...
Mchl Posted October 4, 2009 Share Posted October 4, 2009 You're missing $ in front of $j and $k in lines 12 and 14. Why are you writing your own implementation of this function? Link to comment https://forums.phpfreaks.com/topic/176464-user-defined-array_chunk/#findComment-930179 Share on other sites More sharing options...
lipun4u Posted October 4, 2009 Author Share Posted October 4, 2009 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)); ?> Link to comment https://forums.phpfreaks.com/topic/176464-user-defined-array_chunk/#findComment-930186 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.