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

Php 拆分数组(整数和字符),但每个数组的总和不超过最大值,否则将推送到下一个数组索引,php,arrays,Php,Arrays,我在这里更新这个问题。有两个数组,数组1是具有一系列值的施主数组。数组2是所需的结果,它将存储一系列子数组,这些子数组的值来自数组1,其中每个子数组的总和不超过25。如果它这样做了,多余的将被推送到数组2中的下一个索引,在那里规则也将适用。仅已成功拆分整数值。若数组同时包含整数和字符,则如何拆分 施主阵列(阵列1): $a1=array('aaa10','bbb20','ccc30','ddd40','eee50'); $a1=array(aaa10,bbb20,ccc30,ddd40,eee

我在这里更新这个问题。有两个数组,数组1是具有一系列值的施主数组。数组2是所需的结果,它将存储一系列子数组,这些子数组的值来自数组1,其中每个子数组的总和不超过25。如果它这样做了,多余的将被推送到数组2中的下一个索引,在那里规则也将适用。仅已成功拆分整数值。若数组同时包含整数和字符,则如何拆分

施主阵列(阵列1):

$a1=array('aaa10','bbb20','ccc30','ddd40','eee50');
$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
        )
)
当前输出(数组2):(数组中仅给出整数值)

所需输出(阵列2):

$a1=array('aaa10','bbb20','ccc30','ddd40','eee50');
$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
        )
)
这里是代码

函数slitArray($a1,$num=25)
{
$store=0;
$new=array();
foreach($a1作为$value){
if(是_数组(end($new)))
$sum=数组_sum(当前($new));
其他的
$sum=0;
$count=(count($new)-1);
$i=($count$num){
$use=($num-$sum);
$store=($value-$use);
如果($store>$num){
$divide=函数($store,$num)
{
如果($store>$num){
$count=ceil($store/$num);
对于($i=0;$i$num)?$num:$store;
$store-=$num;
}
退回$new;
}
其他的
返回数组($store);
};
$forward=$divide($store,$num);
$a=$i;
foreach($aVal远期){
$new[$a+=1][]=$aVal;
}
}
否则{
$new[$i+1][]=$store;
$store=0;
}
}
其他的
$use=$value;
如果($use>0)
$new[$i][]=$use;
}
退回$new;
}
$a1=阵列(10,20,30,40,50);
$arr=滑道($a1);
印刷费($arr);

这是修改后的
splitArray
函数:(阅读内联注释进行解释)


这里是修改后的
splitArray
函数:(阅读内联注释进行解释)


在所需的输出中,我给出了like key-value对,这无关紧要,它还可以像**数组([0]=>Array([0]=>aaa10[1]=>bbb15))**在所需的输出中,我给出了类似的键值对,这无关紧要,它还可以像**数组([0]=>Array([0]=>aaa10[1]=>bbb15))**
<?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;
}