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 3 years have passed since last update.

PHP 配列まとめ

Last updated at Posted at 2019-12-31

目的

  • PHPで配列に値を格納して取り出す方法をまとめる

書き方の例

  • 配列に格納し任意の値を出力する方法を記載する。

$変数A = array("値A", "値B", "値C");

//値Aを出力
echo $変数A[0];

//値Bを出力
echo $変数A[1];

//値Cを出力
echo $変数A[2];

//配列の末尾に値Dを追加するしたい時
$変数A[] = "値D";

//値Aを値Eで上書きたいとき
$変数A[0] = "値E";
  • 配列に格納された数値を全て足して表示する方法を下記に記載する。
$all = 0;
foreach ($prices as $price) {
  $all += $price;
}
echo $all

PHP 5.4以降の例

  • 下記にPHP5.4以降の配列の書き方の例を記載する。
$変数A = ["値A", "値B", "値C"];
  • valueの値を区切り文字を指定して出力する。
$変数A = ["値A", "値B", "値C"];

echo implode("区切り文字", 変数A);
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?