0
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 2020-03-19

#配列の中身を確認する

「配列の中身をパパっと確認したいんだけど!?」
ってなったので調べてみた。

print_r()var_dump()って関数を使うといいっぽい。

##print_r()var_dump()の違い
###print_r()で出力

  • 配列であるという情報
  • 各要素の値

###var_dump()で出力

  • 配列全体の要素数
  • 各要素のデータ型
  • 各要素の値のバイト数
  • 各要素の値

var_dump()はデータ型も加味した情報を確認したいときに便利みたい。

##print_r()で出力
####通常配列

$data = ['yamada', 'takahashi'];

print_r ($data);

//実行結果
Array (
 [0] => yamada
 [1] => takahashi
)

####連想配列

$data = ['yamada' => 123, 'takahashi' => 456];

print_r ($data);

//実行結果
Array (
 [yamada] => 123
 [takahashi] => 456
)

####多次元配列

$data = [
  [
    'name' => '山田',
    'age' => 123,
    'sex' => '男',
  ],
  [
    'name' => '高橋',
    'age' => 345,
    'sex' => '女'
  ],
];

print_r ($data);

//実行結果

Array (
 [0] => Array (
   [name] => 山田
   [age] => 123
   [sex] => 
 )
 [1] => Array (
   [name] => 高橋
   [age] => 345
   [sex] =>  )
)

##var_dump()で出力
####通常配列

$data = ['yamada', 'takahashi'];

var_dump($data);

//実行結果
array(2) {
 [0]=> string(6) "yamada"
 [1]=> string(9) "takahashi"
 }

####連想配列

$data = ['yamada' => 123, 'takahashi' => 456];

var_dump($data);

//実行結果
array(2) {
 ["yamada"]=> int(123)
 ["takahashi"]=> int(456)
 }

####多次元配列

$data = [
  [
    'name' => '山田',
    'age' => 123,
    'sex' => '男',
  ],
  [
    'name' => '高橋',
    'age' => 345,
    'sex' => '女'
  ],
];

var_dump($data);

//実行結果
array(2) {
 [0]=> array(3) {
 ["name"]=> string(6) "山田"
 ["age"]=> int(123)
 ["sex"]=> string(3) "男"
 }
 [1]=> array(3) {
 ["name"]=> string(6) "高橋"
 ["age"]=> int(345)
 ["sex"]=> string(3) "女"
 }
}

##おわり
ブラウザだと改行が省略されて表示されるので、ソースを表示機能を使って確認すると見やすい。

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?