0
0

More than 1 year has passed since last update.

array_flip(): Can only flip string and integer values, entry skipped エラーが出た時の対処法

Posted at

エラーメッセージ

array_flip(): Can only flip string and integer values, entry skipped

//文字列と整数値のみを反転できます。入力はスキップされます

問題のコード

.php
$allCookie = $request->cookie();
$allCookie = array_flip($allCookie);

原因

array_flip()は配列(連想配列)のキーとバリューを入れ替えるメソッド。
array_flip()は文字列と整数値のみしか扱えない。

$request->cookie()でcookieを取得したが、内容は下記でNULL値が存在したためエラーとなった。

^ array:4 [▼
  "ABC_asdfpipoij3" => null
  "CLA_sdfgsg" => null
  "XSRF-TOKEN" => "RRY2bgJ1h2onOYmsdfgsdfSmTVa2OrdrNNBaXYDsg8PnBOp"
  "session" => "6r2FazkYws4asdrIeStTuBzasdfoHLj9wjqciNPtToYYo2D"
]

対処法

array_filterNULLを取り除く

.php
$allCookie = $request->cookie();

$allCookie = array_filter($allCookie, function ($value) { return $value !== null; });

$allCookie = array_flip($allCookie);

参照

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