拆分数组(包括整数和字符)但每个数组的总和不超过最大值,否则推送到下一个数组索引

时间:2016-10-07 04:54:47

标签: php arrays

我在这里更新这个link问题。有两个数组,数组1是一个具有一系列值的施主数组。数组2是期望的结果,并且将存储一系列具有来自数组1的值的子数组,其中每个子数组的总和不会超过25.如果确实如此,超出部分将被推送到数组2中的下一个索引,其中规则将是也适用。已经只有整数值成功分割。如果数组同时包含整数和字符,则可以拆分。

捐赠者阵列(阵列1):

$a1=array('aaa10','bbb20','ccc30','ddd40','eee50');

当前输出(数组2):(仅在数组中给出整数值)

$a1=array(10,20,30,40,50);
Array
(
    [0] => Array
        (
            [0] => 10
            [1] => 15
        )

    [1] => Array
        (
            [0] => 5
            [1] => 20
        )

    [2] => Array
        (
            [0] => 10
            [1] => 15
        )

    [3] => Array
        (
            [0] => 25
        )

    [4] => Array
        (
            [0] => 25
        )

    [5] => Array
        (
            [0] => 25
        )

)

所需的输出(数组2):

$a1=array(aaa10,bbb20,ccc30,ddd40,eee50);
Array
(
    [0] => Array
        (
            [aaa] => 10
            [bbb] => 15   
        )

    [1] => Array
        (
            [bbb] => 5
            [ccc] => 20
        )

    [2] => Array
        (
            [ccc] => 10
            [ddd] => 15
        )

    [3] => Array
        (
            [ddd] => 25
        )
    [4] => Array
        (
            [eee] => 25
        )
    [5] => Array
        (
            [eee] => 25
        )
)

此处代码

function slitArray($a1,$num = 25)
{
  $store  =   0;
  $new    =   array();

  foreach($a1 as $value) {

    if(is_array(end($new)))
      $sum    =   array_sum(current($new));
    else
      $sum    =   0;

    $count      =   (count($new)-1);
    $i          =   ($count <= 0)? 0 : $count;

    if(($sum + $value) > $num) {
      $use            =   ($num-$sum);
      $store          =   ($value-$use);
      if($store > $num) {
    $divide =   function($store,$num)
      {
        if($store > $num) {
          $count  =   ceil($store/$num);
          for($i=0; $i<$count; $i++) {
        $new[]  =   ($store > $num)? $num : $store;
        $store  -=  $num;
          }

          return $new;
        }
        else
          return array($store);
      };

    $forward    =   $divide($store,$num);
    $a          =   $i; 
    foreach($forward as $aVal) {
      $new[$a+=1][]   =   $aVal;
    }
      }
      else {
    $new[$i+1][]    =   $store;
    $store          =   0;
      }
    }
    else
      $use    =   $value;

    if($use > 0)
      $new[$i][]      =   $use;
  }

  return $new;
}
$a1=array(10,20,30,40,50); 
$arr=slitArray($a1);
print_r($arr);

1 个答案:

答案 0 :(得分:0)

以下是修改后的splitArray函数:(阅读内联注释以获取解释)

<?php

$a1 = array('aaa10','bbb20','ccc30','ddd40','eee50');

print_r(splitArray($a1, 25));

function splitArray($array, $max_sum) {

    // Create array with alpha key and numeric value
    array_walk($array, function($str) use (&$arr_formatted) {
        $arr_formatted[preg_replace('/[0-9]+/', '', $str)] = filter_var($str, FILTER_SANITIZE_NUMBER_INT);
    });

    $resultantArr = [];

    // Function to fill the resulant array
    $array_operation = function($key, $no, $isMemory = FALSE) use(&$resultantArr, $max_sum, &$memory) {
        if($no <= $max_sum) {
            $resultantArr[][$key] = $no;
            // When the resultant array is filled with data in memory, clear the memory (no more needed!)
            if($isMemory)
                unset($memory[$key]);
        } else {
            $resultantArr[][$key] = $max_sum;
            $memory[$key] = $no - $max_sum;
        }
    };

    foreach ($arr_formatted as $key => $no) {
        if(count($resultantArr) == 0) {
            // Only for first iteration
            $array_operation($key, $no);
        } else {
            // Sum of the last element in array
            $sum = array_sum($resultantArr[key($resultantArr)]);
            if($sum <= $max_sum) {
                if(($no + $sum) <= $max_sum) {
                    $resultantArr[key($resultantArr)][$key] = $no;
                } else {
                    // When the count increases when the number is added, add the number that it can allocate
                    // Remaining is pushed to memory
                    $allocation = $max_sum - $sum;
                    if($allocation > 0) {
                        $resultantArr[key($resultantArr)][$key] = $allocation;
                    }
                    $memory[$key] = $no - $allocation;
                }
            } else {
                // Add the number to new index
                $array_operation($key, $no);
            }
        }

        // Allocate the values stored in memory
        while(isset($memory) && count($memory)) {
            foreach ($memory as $m_key => $m_no) {
                $array_operation($m_key, $m_no, TRUE);
            }
        }

        // Move the pointer to the last location
        end($resultantArr);
    }

    return $resultantArr;
}

查看工作结果here