LoginSignup
0
0

More than 1 year has passed since last update.

PHPで配列を使ってみましょう!

Posted at

配列について



PHPの配列を操作していきます!

以下の $scores に 1, 2, 3 の値を代入したいとします。

配列は[ ]で囲って、 , で区切ります。

index.php

<?php
$scores = [1, 2, 3];

echo $scores[2] . PHP_EOL;



以下をターミナルで実行します!

~$ php index.php



$scores の [ 2 ] の要素が出力されました!

~ $ php index.php
3
~ $ 



インデックス番号は 0 から始まるので、

0番目→1
1番目→2
2番目→3

と表示されます!
なので $scores [ 2 ] は 3 が表示されました!



配列を置き換える時にもインデックス番号を使います。
$scores[ 2 ] を8に置き換えてみます。

index.php
<?php
$scores = [1, 2, 3];

$scores[2] = 8;
echo $scores[2] . PHP_EOL;



ターミナルで以下を実行します...

~$ php index.php



置き換わりました!

~ $ php index.php
8
~ $ 
0
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
0
0