Home>

I want to extract the process of writing as follows in a loop. How can I write it?

echo $array [0] [0] [0];
  echo $array [0] [1] [0];
  echo $array [0] [2] [0];
  echo $array [0] [3] [0];
  echo $array [0] [...] [0];
  echo $array [1] [0] [0];
  echo $array [1] [1] [0];
  echo $array [1] [2] [0];
  echo $array [1] [3] [0];
  echo $array [1] [...] [0];
php
  • Answer # 1

    Basic foreach should be used

    $a = [
      [
        ["x1"],
        ["x2"],
        ["x3"],
        ["x4"],
        ], [
        ["y1"],
        ["y2"],
        ["y2"],
        ["y4"],
          ],
      ];
    print_r ($a);
    foreach ($a as $a1) {
      foreach ($a1 as $a2) {
        foreach ($a2 as $val) {
          print $val;
        }
      }
    }


    However, if you want to exclude things you don't want to see, don't show them all
    Processing to specify data is required

  • Answer # 2

    If given conditions (3D array, 1st dimension: 0 to 1, 2nd dimension: indefinite length, 3rd dimension: 0 fixed) ,

    for ($i = 0;$i<2;$i ++) {
        for ($j = 0;$j<count ($array [$i]);$j ++) {
            echo "\ $array [$i] [$j] [0] \ n";
            // echo $array [$i] [$j] [0]. PHP_EOL;
        }
    }

    Foreach isNGbecause the order is not guaranteed even in a subscript array.

  • Answer # 3

    In your favorite loop
    ・ Foreach
    ・ For
    ・ While
    ・ Recursive function
    Which is okay?

    The configuration is almost the same except for recursive functions.
    I think it's interesting to play with recursive functions.

  • Answer # 4

    A php-like guy.

    <? php
    $a = [
      [
        ["x1"],
        ["x2"],
        ["x3"],
        ["x4"],
        ], [
        ["y1"],
        ["y2"],
        ["y2"],
        ["y4"],
          ],
      ];
    function vol_echo ($vol, $key)
    {
        echo $vol.PHP_EOL;
    }
    array_walk_recursive ($a, 'vol_echo');