LoginSignup
1
0

More than 3 years have passed since last update.

文字列・配列の変換など

Posted at

文字列を配列に変換 

qiita.php
$menu = 'プリン,ショートケーキ,ガトーショコラ,いちごパフェ,パンケーキ';
    $menu = explode(",", $menu);
// , を配列の要素の区切りとする。
    print_r($menu);
//Array ( [0] => プリン [1] => ショートケーキ [2] => ガトーショコラ [3] => いちごパフェ [4] => パンケーキ )

配列を文字列に変換

qiita.php
$menu = implode($menu);
echo $menu;
//プリンショートケーキガトーショコラいちごパフェパンケーキ

//引数に区切りを指定できる
$menu = implode(",", $menu);
echo $menu;
//プリン,ショートケーキ,ガトーショコラ,いちごパフェ,パンケーキ

文字列に区切り(、 とか)がないとき

qiita.php
$num = "01234567890";
    $num = str_split($num);
    print_r($num);
//Array ( [0] => 0 [1] => 1 [2] => 2 [3] => 3 [4] => 4 [5] => 5 [6] => 6 [7] => 7 [8] => 8 [9] => 9 [10] => 0 )

//第2引数に文字の長さを指定
$num = "01234567890";
    $num = str_split($num, 3);
    print_r($num);
//Array ( [0] => 012 [1] => 345 [2] => 678 [3] => 90 )

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