在PHP中使用array_chunk移动元素 [英] Move elements using array_chunk with PHP

查看:142
本文介绍了在PHP中使用array_chunk移动元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个基本数组,在其中使用array_chunk将其分为3个元素.

$array = array(
    'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'
);

$chunk = array_chunk($array, 3);

结果如下

[
   [
     "a",
     "b",
     "c"
   ],
   [
     "d",
     "e",
     "f"
   ],
   [
     "g",
     "h"
   ]
]

(Las块具有2个元素)

如果最后一个块"只有2个元素,我如何向下移动第一个块的一个元素,以便第一个元素具有2个?

它应该看起来像这样:

[
   [
     "a",
     "b",
   ],
   [
     "c"
     "d",
     "e",
   ],
   [
     "f"
     "g",
     "h
   ]
]

(第一个块包含2个元素)

解决方案

最简单的方法是erray_reverse的多重执行:首先反转整个数组,然后拆分,然后反转每个块,最后是块数组.

单线,它看起来像这样:$chunk = array_reverse(array_map('array_reverse', array_chunk(array_reverse($array), 3)));

但是请注意,反转数组是一项昂贵的操作,因此,如果数组比示例中的数组更实际,则不建议这样做.


更有效,但也有更多代码: 使用模计算需要在第一个块中的元素数: $first_chunk_size = count($array) % 3;

接下来,如果数组大小是块大小的倍数,请避免使用空数组,如果取模为0,请更正块大小: $first_chunk_size = $first_chunk_size == 0 ? 3 : $first_chunk_size;

然后,切除第一部分: $first_chunk = array_slice($array, 0, $first_chunk_size);

接下来,拆分数组的其余部分: $chunks = array_chunk(array_slice($array,$first_chunk_size),3);

然后将第一个块和其他块组合到一个数组中: array_unshift($chunks, $first_chunk);

第二种方法的完整代码:

$array = [
    'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'
];

$first_chunk_size = count($array) % 3;
$first_chunk_size = $first_chunk_size == 0 ? 3 : $first_chunk_size;

$first_chunk = array_slice($array, 0, $first_chunk_size);
$chunks = array_chunk(array_slice($array,$first_chunk_size),3);
array_unshift($chunks, $first_chunk);

var_dump($chunks);

(显然,可以通过不一步一步做所有事情来简化此代码)


编辑:在阅读完您对其他答复的评论后,您可以对此方法进行一些修改,然后将其转变为可恢复的功能.进行一些代码清理.它可能看起来像这样:

$array = [
    'a', 'b', 'c', 'd', 'e', 'f', 'g'
];

function custom_array_chunk ($array) {
    $first_chunk_size = count($array) % 3;
    if ($first_chunk_size == 0) {
        $chunks = array_chunk($array, 3);
    } else {
        $chunks = custom_array_chunk(array_slice($array,2));
        array_unshift($chunks, array_slice($array, 0, 2));
    }
    return $chunks;
}

var_dump(custom_array_chunk($array));

这将适用于两种情况,即1个和2个元素剩余",并且将完全产生您告诉我们的内容.

演示

I have a basic array in which I am using array_chunk to divide it into 3 elements.

$array = array(
    'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'
);

$chunk = array_chunk($array, 3);

The result is as follows

[
   [
     "a",
     "b",
     "c"
   ],
   [
     "d",
     "e",
     "f"
   ],
   [
     "g",
     "h"
   ]
]

(Las chunk have 2 elements)

In case the last "chunk" has only 2 elements, how can I move down an element of the first chunk so that the first element has 2?

It should look like this:

[
   [
     "a",
     "b",
   ],
   [
     "c"
     "d",
     "e",
   ],
   [
     "f"
     "g",
     "h
   ]
]

(First chunk have 2 elements)

解决方案

The easiest way are mutliple executions of erray_reverse: first reverse the entire array, then split, then reverse every chunk and then, lastly, the array of chunks.

As a one-liner, it loooks like this: $chunk = array_reverse(array_map('array_reverse', array_chunk(array_reverse($array), 3)));

Mind however that reversing an array is an expensive operation, so if you array is acutally alrger than in your example, this is not a recommended way.


More effective but also more code: Calculate the number of elements need to be in the first chunk using modulo: $first_chunk_size = count($array) % 3;

Next, to avoid an empty array if array size is a multiple of chunk size, correct the chunk size if modulo yields 0: $first_chunk_size = $first_chunk_size == 0 ? 3 : $first_chunk_size;

then, cut off the first part: $first_chunk = array_slice($array, 0, $first_chunk_size);

next, split the rest of the array: $chunks = array_chunk(array_slice($array,$first_chunk_size),3);

then combine the first chunk and the other chunks into one array: array_unshift($chunks, $first_chunk);

Full code of the second approach:

$array = [
    'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'
];

$first_chunk_size = count($array) % 3;
$first_chunk_size = $first_chunk_size == 0 ? 3 : $first_chunk_size;

$first_chunk = array_slice($array, 0, $first_chunk_size);
$chunks = array_chunk(array_slice($array,$first_chunk_size),3);
array_unshift($chunks, $first_chunk);

var_dump($chunks);

(obviously this code can be simplified by not doing everything step-by-step)


EDIT: Having read your comments to the other replies, you can modify this approach a little and turn it into a recusrive function. With some code-cleanup,. it may look like this:

$array = [
    'a', 'b', 'c', 'd', 'e', 'f', 'g'
];

function custom_array_chunk ($array) {
    $first_chunk_size = count($array) % 3;
    if ($first_chunk_size == 0) {
        $chunks = array_chunk($array, 3);
    } else {
        $chunks = custom_array_chunk(array_slice($array,2));
        array_unshift($chunks, array_slice($array, 0, 2));
    }
    return $chunks;
}

var_dump(custom_array_chunk($array));

This will work for both cases, 1 and 2 elements "left over" and will yield exactly what you told us.

Demo

这篇关于在PHP中使用array_chunk移动元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆