Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Semrush - keyword research tool

Turn ChatGPT, Claude, Gemini, And CoPilot Into Your Personal Assistant, Business Coach, Content Creator, And More

AFFILIATE MARKETING Your all-in-one performance engine Manage affiliates, creators, and customer referrals in one unified platform—turning every partnership into measurable growth
Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

42,705 questions

55,464 answers

573 users

How to split an array and add the first part to end in PHP

1 Answer

0 votes
function print_arr($arr) {
	$size = count($arr);

	for ($i = 0; $i < $size; $i++) {
		echo $arr[$i] . " ";
	}
	echo "\n";
}

function reverse(&$arr, $start, $end) {
	for ($i = $start, $j = $end; $i <= $end && $j > $i; $i++, $j--) {
		$temp = $arr[$i];
		$arr[$i] = $arr[$j];
		$arr[$j] = $temp;
	}
}

function split(&$arr, $split_point) {
    $size = count($arr);
	if ($size <= 1 && $split_point < 1 && $split_point >= $size) {
		return;
	}
	
	// reverse first part
	reverse($arr, 0, $split_point - 1);

	// reverse second part
	reverse($arr, $split_point, $size - 1);

	// reverse all array 
	reverse($arr, 0, $size - 1);
}


$arr = array(1, 2, 3, 4, 5, 6, 7, 8, 9, 0);
	
$split_point = 3;
	
split($arr, $split_point);

print_arr($arr);



	
/*
run:
   
4 5 6 7 8 9 0 1 2 3 
   
*/

 



answered Nov 29, 2021 by avibootz

Related questions

1 answer 276 views
1 answer 268 views
1 answer 240 views
1 answer 263 views
1 answer 182 views
...