Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
Help us understand the problem. What is going on with this article?

php 標準入力の値をforeach文で表示

個人学習用にメモ

標準入力の値を、foreach文で表示する。
※あとでまとめる用

$input_line = fgets(STDIN);
    while($input_line = trim(fgets(STDIN))){
        $array[] = explode(" ",$input_line);//2次元配列で横の行を読み込み
    }

    print_r($array);

    foreach ($array as $key1 => $value1) {
      foreach ($value1 as $key2 => $value2) {
          if ($key2 == 1){
               $value2 = $value2 + 1;//$key2が「1」なら、要素に+1の処理
               print $value2 . "\n"; 
     }else{
          echo $value2." ";//$key2が「0」なら、要素+スペースで表示する
       }
      }
    }

メモ:
外側のforeachが「[0] => Array」の部分をまわす。
内側のforeachが「[0] => tanaka」の部分をまわす。

Array
(
    [0] => Array
        (
            [0] => tanaka
            [1] => 20
        )

    [1] => Array
        (
            [0] => kondo
            [1] => 10
        )

    [2] => Array
        (
            [0] => honda
            [1] => 30
        )

)
tanaka 21
kondo 11
honda 31

17end
jquery を学習中の初心者です。よろしくお願いいたします。
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away