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

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

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

问题

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

  1. <?php
  2. $a = 10; // number of arrays
  3. $b = 3; // number of elements inside array
  4. $c = [];
  5. for ($i = 0; $i < $a; $i++)
  6. {
  7. $d = [];
  8. for ($j = 0; $j < $b; $j++)
  9. {
  10. $d[] = $i + $j;
  11. }
  12. $c[] = $d;
  13. }
  14. print_r($c);
  15. ?>

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

英文:

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

  1. &lt;?php
  2. $a = 9; // number of arrays
  3. $b = 3; // number of elements inside array
  4. $c = [];
  5. for ($i = 0; $i &lt; $a; $i++)
  6. {
  7. $d = [];
  8. for ($j = 0; $j &lt; $b; $j++)
  9. {
  10. $d[] = $i = 0;
  11. }
  12. $c[] = $d;
  13. }
  14. print_r($c);
  15. ?&gt;
  16. [
  17. [0,0,0],
  18. [0,0,1],
  19. [0,1,1],
  20. [1,1,1],
  21. [1,1,2],
  22. [1,2,2],
  23. [2,2,2],
  24. [2,2,3],
  25. [2,3,3],
  26. [3,3,3]
  27. ]

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

答案1

得分: 2

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

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

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

英文:

I managed to get your result with this code:

  1. for ($i = 0; $i &lt; 10; $i++) {
  2. $data[] = [intdiv($i, 3),
  3. intdiv($i + 1, 3),
  4. intdiv($i + 2, 3)];
  5. }
  6. 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:

确定