创建一个整数数组,从最后一个元素递减到第一个元素。

huangapple go评论60阅读模式
英文:

how to create such an int array that increase by one from the last to the first element

问题

以下是使用PHP实现与您提供的数组相同的数组的代码:

<?php

$a = 10; // number of arrays
$b = 3; // number of elements inside array
$c = [];

for ($i = 0; $i < $a; $i++)
{
    $d = [];
    
    for ($j = 0; $j < $b; $j++)
    {
        $d[] = $i + $j;
    }
    
    $c[] = $d;
}

print_r($c);
?>

这段代码将生成与您提供的数组相同的数组,并输出结果。希望这可以帮助您解决问题。

英文:

how to achieve the same array down bellow with php i have that code

&lt;?php

$a = 9; // number of arrays
$b = 3; // number of elements inside array
$c = [];

for ($i = 0; $i &lt; $a; $i++)
{
    $d = [];
    
    for ($j = 0; $j &lt; $b; $j++)
    {
        $d[] = $i = 0;
    }
    
    $c[] = $d;
}

print_r($c);
?&gt;


[

[0,0,0],
[0,0,1],
[0,1,1],
[1,1,1],
[1,1,2],
[1,2,2],
[2,2,2],
[2,2,3],
[2,3,3],
[3,3,3]
]

i tried alot of mathematic ways with no success can you please help me

答案1

得分: 2

我用这段代码成功获取到了你的结果:

for ($i = 0; $i < 10; $i++) {
    $data[] = [intdiv($i, 3), 
               intdiv($i + 1, 3), 
               intdiv($i + 2, 3)];
}

var_export($data);

参考链接:https://3v4l.org/NChUj

英文:

I managed to get your result with this code:

for ($i = 0; $i &lt; 10; $i++) {
    $data[] = [intdiv($i, 3), 
               intdiv($i + 1, 3), 
               intdiv($i + 2, 3)];
}

var_export($data);

See: https://3v4l.org/NChUj

huangapple
  • 本文由 发表于 2023年7月20日 16:14:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/76727899.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定