LoginSignup
0
1

More than 3 years have passed since last update.

テーブルに非スカラ(第1正規形未満、複数の値を文字列で格納)があったらexplode()を使う

Last updated at Posted at 2019-08-08

いろいろな事情であるカラムがこういうときありますよね。

id person_ids
1 1,2,3
2 4,6
3 7,8,10

ひとつのフィールド(エクセルでいうとセル)に複数の値が入っていると、get()した値をその後加工するとき大変です。

そんなときは、こちらのメソッド。

$person_ids = '1, 2, 3';
$person_ids = explode(',', $person_ids);

// result
array(3) {
  [0]=>
  string(1) "1"
  [1]=>
  string(1) "2"
  [2]=>
  string(1) "3"
}

もし、フィールドに余計な文字列などがあったらpreg_replace()等でテキストを整えてからやるとよい。

php.net
explode
https://www.php.net/manual/ja/function.explode.php

0
1
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
1