1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

【PHP基礎⑱】配列-2.配列の初期化

Last updated at Posted at 2022-06-27

[問題]  (参照:http://www.cc.kyoto-su.ac.jp/~mmina/bp1/hundredKnocksBasic.html)
要素数10の整数型の配列を宣言し、整数値を入力させ、すべての配列の要素を入力値として、すべての要素の値を表示するプログラムを作成しなさい。

コード

$a = [1,2,3,4,5,6,7,8,9,10];
$m = intval(fgets(STDIN));
$k = [];
foreach($a as $i){
    $k[] = $m;
}
foreach($k as $r){
    echo $r, PHP_EOL;
}

↓「5」と入力

結果

5
5
5
5
5
5
5
5
5
5

☆配列の初期化
$k = [];

☆配列に新しい要素を追加する
$k[] = $m;
→キーを指定しない場合は、自動で整数のキーが付与される。
現在ある最大のキーに1を加えたものが与えられるが、キーが1つもない時は0から付与される。

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?