基于Php Split数组的vales

时间:2018-04-30 12:00:45

标签: php

输入:给定动态值数组。

对于EX:

I =阵列(1,2,3,4,5,6,7,8,9);

预期输出:(i的数组)/ n

对于EX :(数组i / 3)

1)1,2,3

2)4,5,6

3)7,8,9

如何做到这一点

由于

2 个答案:

答案 0 :(得分:0)

我没有得到你的观点,但你可以这样尝试

$sourceArray = Array('1','2','3','4','5','6','7','8','9');
$arr1 = array_slice($sourceArray, 0, (count($sourceArray)/2));
$arr2 = array_slice($sourceArray, (count($sourceArray)/2));


print_r($sourceArray);
//========= FIRST ARRAY ============;
echo"first array";
echo "</br>";
echo $arr1[0].','.$arr1[1].','.$arr1[2].','.$arr1[3];
echo "</br>";

//========= FIRST ARRAY ============;
echo"scond array";
echo "</br>";
echo $arr2[0].','.$arr2[1].','.$arr2[2].','.$arr2[3];

答案 1 :(得分:0)

你好@Mono你可以这样做

$sourceArray = Array('1','2','3','4','5','6','7','8','9');

 $list=$sourceArray;
 $p=3;


    $listlen = count($list);
    $partlen = floor($listlen / $p);
    $partrem = $listlen % $p;
    $partition = array();
    $mark = 0;
    for($px = 0; $px < $p; $px ++) {
        $incr = ($px < $partrem) ? $partlen + 1 : $partlen;
        $partition[$px] = array_slice($list, $mark, $incr);
        $mark += $incr;
    }
    print_r($partition);