【问题标题】:How to split array into two different array如何将数组拆分为两个不同的数组
【发布时间】:2014-07-31 15:01:21
【问题描述】:

如何将数组拆分为两个不同的数组并返回。这里我的数组是 $input ,它可以包含任意数量的元素。

例如:

$input = array ('onex','twox','threex','fourx','fivex','sixx','sevenx','eightx','ninex');

我想将我的 '$input' 数组拆分为两个不同的数组 '$number1' 和 '$number2' 。

1)如果$input数组元素为偶数,则拆分为2个相等元素的数组。

2)如果 $input 数组具有奇数元素,则 '$number1' 总是比 '$number2' 大 1 个元素。

【问题讨论】:

    标签: php arrays slice


    【解决方案1】:

    您可以使用array_chunk。

    $new_arrays = array_chunk($input, ceil(count($input)/2));
    $number1 = $new_arrays[0];
    $number2 = $new_arrays[1];
    

    【讨论】:

      猜你喜欢
      • 2018-06-17
      • 1970-01-01
      • 1970-01-01
      • 2012-04-13
      • 2020-10-22
      • 2015-05-17
      • 1970-01-01
      • 2016-06-27
      • 1970-01-01
      相关资源
      最近更新 更多