Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can i take an array, divide it by two and create two lists?

Tags:

arrays

php

Say i have an array

$array 

Could anyone give me an example of how to use a foreach loop and print two lists after the initial array total has been counted and divided by two, with any remainder left in the second list?

So instead of just using the foreach to create one long list it will be creating two lists? like so...

  1. Value 1
  2. Value 2
  3. Value 3

and then the second list will continue to print in order

  1. Value 4
  2. Value 5
  3. Value 6
like image 482
Andy Avatar asked Mar 22 '11 14:03

Andy


People also ask

How do you divide an array into two parts?

To divide an array into two, we need at least three array variables. We shall take an array with continuous numbers and then shall store the values of it into two different variables based on even and odd values.

How do you split an array into two sets in Python?

A Simple solution is to run two loop to split array and check it is possible to split array into two parts such that sum of first_part equal to sum of second_part.

Can you divide an array by an array?

The np. divide() is a numpy library function used to perform division amongst the elements of the first array by the elements of the second array. The process of division occurs element-wise between the two arrays. The numpy divide() function takes two arrays as arguments and returns the same size as the input array.


1 Answers

To get a part of an array, you can use array_slice:

$input = array("a", "b", "c", "d", "e");  $len = count($input);  $firsthalf = array_slice($input, 0, $len / 2); $secondhalf = array_slice($input, $len / 2); 
like image 116
Mormegil Avatar answered Sep 22 '22 15:09

Mormegil