Ccmmutty logo
Commutty IT
1 min read

[PHP] 配列の要素すべてをサクッと出力する方法

https://cdn.magicode.io/media/notebox/blob_IS0GRMk
PHPで配列のすべて要素を確認するとき、よく foreach などで順次ループしながら print() で確認していました。
これだとちょっとめんどくさいので、サクッと確認できるように考えました。

やり方

print_r() を使います。
javascript
<?php

 $array =array( '1' => 'A' , '2' => 'B', '3' => 'C');
 $result = print_r($array, true);
 echo $result;

?>

1:1 - Expression expected.
1:2 - Expression expected.
1:3 - Cannot find name 'php'.
2:2 - ':' expected.
2:2 - Cannot find name '$array'. Did you mean 'Array'?
2:10 - Cannot find name 'array'. Did you mean 'Array'?
2:21 - ',' expected.
2:34 - ',' expected.
2:46 - ',' expected.
3:2 - Cannot find name '$result'.
3:12 - Cannot find name 'print_r'.
3:20 - Cannot find name '$array'. Did you mean 'Array'?
4:2 - Cannot find name 'echo'.
4:7 - ';' expected.
4:7 - Cannot find name '$result'.
5:1 - Declaration or statement expected.
5:2 - Expression expected.
5:3 - Expression expected.
実行すると、こんな感じで出力されます。
Array
(
    [1] => A
    [2] => B
    [3] => C
)

Discussion

コメントにはログインが必要です。