英文:
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
<?php
$a = 9; // 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 = 0;
}
$c[] = $d;
}
print_r($c);
?>
[
[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 < 10; $i++) {
$data[] = [intdiv($i, 3),
intdiv($i + 1, 3),
intdiv($i + 2, 3)];
}
var_export($data);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论