Title: Distribute php loop values in columns
Tags: php;for-loop
Question: I have table having 3 column says customer 1,customer 2 and customer 3, Having upper limit value like 200,300 and 500 total 1000.And i have 8 different values in an array like 150,150,150,150,100,100,100,50,50 and i have to distribute these values in to each customers as better as posible(dont cross limit).
I tried to make table before split
Before Split
<?php
$customer_count = 3;
$customer_limit = array('200','300','500');
$values_array = array('150','150','150','150','100','100','100','50','50')
<table width="50%" border="1"&gt;
<tr&gt;
<?php
for($i=1;$i<=$customer_count;$i++){
echo "<td&gt;Customer ".$i."</td&gt;";
?&gt;
</tr&gt;
<tr&gt;
<?php
for($i=0;$i<$customer_count;$i++){
echo "<td&gt;".$customer_limit[$i]."</td&gt;";
?&gt;
</tr&gt;
<tr&gt;
<td&gt;
<table width="100%" border="1"&gt;
<?php
foreach($values_array as $val){
?&gt;
<tr&gt;
<td&gt;<?=$val;?&gt;</td&gt;
</tr&gt;
<?php
}
?&gt;
</table&gt;
</td&gt;
<td&gt;
<table width="100%" border="1"&gt;
<?php
foreach($values_array as $val){
?&gt;
<tr&gt;
<td&gt;<?=$val;?&gt;</td&gt;
</tr&gt;
<?php
}
?&gt;
</table&gt;
</td&gt;
<td&gt;
<table width="100%" border="1"&gt;
<?php
foreach($values_array as $val){
?&gt;
<tr&gt;
<td&gt;<?=$val;?&gt;</td&gt;
</tr&gt;
<?php
}
?&gt;
</table&gt;
</td&gt;
</tr&gt;
</table&gt;
So i give me an idea how to make "after split" table like in the picture.

Here is the accepted answer: You need not use so many loops here. My approach is a bit different, but it certainly solves the purpose.
We need a nested loop, one for the customers and one for the values.
First, we can loop through and store data in a variable called $customer, which stores data of all 3 customers with their respective distributed values(Keeping in mind their max limit).
for ($i = 0; $i < count($customer_limit); $i++) {
foreach ($values_array as $k =&gt; $value) {
$pre_existing = isset($customer[$i]) ? array_sum($customer[$i]) : 0; // If customer has any previous value, calculate the sum, or else make it 0(for the first time).
if (($pre_existing+$value) <= $customer_limit[$i]) {  // Existing value + the current value must be less than the specified customer limit
$customer[$i][] = @$value;    // Values are stored
unset($values_array[$k]);    // Currently used value is deleted from the original array, considering the value used by one customer can't be used by any other customer.
}
echo "<pre&gt;";
print_r($customer);
Details of three customers:
[0] =&gt; Array
[0] =&gt; 150
[1] =&gt; 50
[1] =&gt; Array
[0] =&gt; 150
[1] =&gt; 150
[2] =&gt; Array
[0] =&gt; 150
[1] =&gt; 100
[2] =&gt; 100
[3] =&gt; 100
[4] =&gt; 50
Comment for this answer: Yes this is what i am looking for...how can we display it as in a table form
Comment for this answer: can u solve these http://stackoverflow.com/questions/38352525/distribute-array-value-in-table-column


{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 1, 'CommentCount': 0, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2016:00:07/12/16', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<php><for-loop>', 'Title': 'Distribute php loop values in columns', 'ViewCount': 147, 'id': 38324101, 'user_AboutMe': '', 'user_AccountId': 6106834.0, 'user_DisplayName': 'Jasir alwafaa', 'user_Reputation': 586.0, 'user_Views': 61.0, 'user_id': 4764352.0}}