将使用值递增列表数组中的嵌套数组的PHP方法
P粉005134685
P粉005134685 2024-03-22 11:07:40
0
1
415

可能看不到东西, 我有两个数组:

$grid =
      
    Array
    (
        [0] => 3
        [1] => 2
        [2] => 3
        [3] => 2
    )

$elements =   

 Array
    (
        [0] => 24426
        [1] => 25015
        [2] => 24422
        [3] => 24425
        [4] => 24531
        [5] => 24421
        [6] => 24530
        [7] => 24532
        [8] => 25016
        [9] => 24418
    )

基本上,这个想法是为 $grid 的每个值和 $elements 的值提供类似的东西。例如 [0] => 3 循环三次将得到 24426,25015,24422。现在问题来了,对于第二个结果 [1] => 2 我只需要获取两个值,但不包括迭代的三个 $element 的先前值。所以基本上在第二次迭代中我会得到24425,24531。

注意:$grid 值可以是 1 , 2 ,3 ....300...n;

结果数组应该是这样的:

Array
    (
        [0] => 3,24426
        [1] => 3,25015
        [2] => 3,24422
        [3] => 2,24425
        [4] => 2,24531
        [5] => 3,24421
        [6] => 3,24530
        [7] => 3,24532
        [8] => 2,25016
        [9] => 2,24418
  
  )

P粉005134685
P粉005134685

全部回复(1)
P粉667649253

编辑:稍微更改代码以满足所需的输出格式

请考虑此代码。

$grid = [3, 2, 3, 2];
$elements = [24426,25015,24422,24425,24531,24421,24530,24532,25016,24418];

$result = [];
foreach($grid as $take) {
    $org_take = $take;
    while($take-- > 0) {
        if (empty($elements)) {
            throw new Exception('Not enough elements');
        }
        $result[] = sprintf('%d,%d', $org_take, array_shift($elements));
    }
}

print_r($result);

给出结果:

Array ( 
    [0] => 3,24426 
    [1] => 3,25015 
    [2] => 3,24422 
    [3] => 2,24425 
    [4] => 2,24531 
    [5] => 3,24421 
    [6] => 3,24530 
    [7] => 3,24532 
    [8] => 2,25016 
    [9] => 2,24418 
)
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板