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 unset(), implode(), explode(), in_arrayの使い方

Posted at

各関数の意味

unset

変数の割り当てを解除する

implode

配列を引数に指定した文字で区切り文字列として結合する

explode

文字列を指定した値を区切りにして配列に分割する

in_array

配列の中から指定した値があるか確認する

例)

<?php 


//unset
$foo = 'test';
echo $foo."\n";
unset($foo);// 変数を削除する
echo 'hoge'."\n";
echo $foo."unset\n";
echo 'hoge2'."\n";

/*
test
hoge
unset //unset()によってtestunsetとならず「unset」のみがechoされている
hoge2
*/


//implode
$arr = ["one","two","three"];
echo implode($arr)."\n";// onetwothre ←第一引数に分割する値を入れないと配列を単純に結合する。
echo implode(",", $arr)."\n";// one,two,three ←引数を指定した事で第一引数の「,」を配列のインデックスごとに挿入して結合している


//explode
$str = "one,two,three"; // one,two,three
var_dump(explode(",", $str));
/*
array(3) {
  [0]=>
  string(3) "one"
  [1]=>
  string(3) "two"
  [2]=>
  string(5) 
  */
//↑第一引数で指定した「,」で文字列を分割し配列を作っている


$search_arr = ['remon','stroberry','apple'];

if(in_array('apple',$search_arr)){
    echo 'リンゴがあります';
}else{
    echo 'リンゴはありません';
}
//リンゴがあります
//第一引数で指定した値が配列内にあればtrueを返す(なければfalseを返す)。
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?