4
5

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

[php]連想配列の操作まとめ

Last updated at Posted at 2015-07-08

自分用メモ。随時追加予定

初期化

index.php
$fruit = array(
  'apple' => 'ringo',
  'orange' => 'mikan',
);

##変更

index.php
$fruit['apple'] = 'ringo_new';

##追加

index.php
// 書き方1 もし既存要素に同名keyがある場合、上書きされない(新規追加もされない)
$fruit += array( 'grape' => 'budou');
//書き方2 もし既存要素に同名keyがある場合、上書きされる
$fruit['grape'] = 'budou';

@kazu56さんのこのページが詳しいです。
【PHP】連想配列、配列への追加

並び替え

index.php
// key基準で並び替え
ksort($fruit);

#入れ替え

index.php
// key とvalue を入れ替える
$fruit = array_flip($fruit);
4
5
2

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
4
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?