ページの作成
親となるページを選択してください。
親ページに紐づくページを子ページといいます。
例: 親=スポーツ, 子1=サッカー, 子2=野球
子ページを親ページとして更に子ページを作成することも可能です。
例: 親=サッカー, 子=サッカーのルール
親ページはいつでも変更することが可能なのでとりあえず作ってみましょう!
| この記事の要点 |
|---|
|
エラーの状況
// Warning: Illegal offset type in xxx.php on line N
$arr = [];
$obj = new stdClass();
$arr[$obj] = "value"; // ← オブジェクトをキーに → エラー
$key = null;
$arr[$key] = "value"; // ← null も場合により警告
$arrKey = ["a", "b"];
$arr[$arrKey] = "x"; // ← 配列をキーに → エラー
PHP の連想配列のキーは 整数 (int) または文字列 (string) しか使えません。それ以外の型を渡すと Illegal offset type 警告が発生します。
PHP の配列キーで許される / 許されない型
| 型 | キーとして | 挙動 |
|---|---|---|
int | ○ | そのまま整数キー |
string | ○ | そのまま文字列キー(数字のみなら int に自動変換) |
float | △ | 小数点切り捨てで int 変換(PHP 8.1+ で非推奨) |
bool | △ | true → 1, false → 0 に変換 |
null | △ | 空文字列 "" に変換 |
array | × | Illegal offset type 警告 |
object | × | Illegal offset type 警告 |
resource | × | Illegal offset type 警告 |
対処方法
方法 1: オブジェクトを ID 化して文字列キーに
$obj = new stdClass();
$key = spl_object_hash($obj); // 一意な文字列ハッシュ
$arr[$key] = "value";
// または ID 文字列を自分で作る
$key = get_class($obj) . "_" . spl_object_id($obj);
$arr[$key] = "value";
方法 2: オブジェクトのプロパティをキーにする
class User {
public int $id;
public string $name;
}
$user = new User();
$user->id = 123;
$user->name = "Alice";
// id をキーにする
$arr[$user->id] = $user; // OK
方法 3: SplObjectStorage を使う(オブジェクトをキーにできる Map)
$storage = new SplObjectStorage();
$obj1 = new stdClass();
$obj2 = new stdClass();
$storage[$obj1] = "value1"; // OK
$storage[$obj2] = "value2"; // OK
// 取得
echo $storage[$obj1]; // "value1"
// 存在チェック
if (isset($storage[$obj1])) { ... }
// 削除
unset($storage[$obj1]);
// イテレート
foreach ($storage as $obj) {
$value = $storage[$obj];
}
方法 4: 配列キーには文字列に変換
// 配列をキーにしたい場合は JSON 文字列化
$arrKey = ["category" => "books", "year" => 2026];
$key = json_encode($arrKey);
$arr[$key] = "value"; // OK
// または implode
$arrKey = ["books", 2026];
$key = implode("_", $arrKey);
$arr[$key] = "value"; // "books_2026"
よくある発生シーン
シーン 1: SimpleXML や DOM オブジェクトをキーに
$xml = simplexml_load_file("data.xml");
foreach ($xml->item as $item) {
$arr[$item] = $item->value; // ← SimpleXMLElement はオブジェクト → エラー
}
// 修正
foreach ($xml->item as $item) {
$arr[(string)$item->id] = (string)$item->value;
}
シーン 2: フォームから受け取った配列をキーに
// $_POST["categories"] が配列の場合
$key = $_POST["categories"]; // 配列
$arr[$key] = "value"; // ← Illegal offset type
// 修正
foreach ($_POST["categories"] as $cat) {
$arr[$cat] = "value";
}
シーン 3: 関数の戻り値が想定外
function getKey() {
return new stdClass(); // 本来 string を返すべきだった
}
$arr[getKey()] = "value"; // ← エラー
// 修正: 関数の実装を見直すか、結果を変換
$result = getKey();
$key = is_object($result) ? spl_object_hash($result) : (string)$result;
$arr[$key] = "value";
PHP 8 以降での厳格化
PHP 8.0+ では、以前は警告だった一部の操作がエラーになっています:
- PHP 7.x:
Warningとして警告、処理は続行(キーは無視) - PHP 8.0+:
TypeErrorとして例外、処理が止まる
// PHP 8 での捕捉
try {
$arr[$obj] = "value";
} catch (TypeError $e) {
// ハンドリング
}
関連エラー
Undefined offset/Undefined index: 存在しないキーで参照Cannot use object of type X as array: オブジェクトを配列扱いTrying to access array offset on value of type null: null を配列扱いIllegal string offset: 文字列を連想配列扱い
関連記事
ページの作成
親となるページを選択してください。
親ページに紐づくページを子ページといいます。
例: 親=スポーツ, 子1=サッカー, 子2=野球
子ページを親ページとして更に子ページを作成することも可能です。
例: 親=サッカー, 子=サッカーのルール
親ページはいつでも変更することが可能なのでとりあえず作ってみましょう!
子ページ
子ページはありません
同階層のページ
- SQLSTATE[HY000] [1045] Access denied for user 'homestead'@'localhost'
- Add [~] to fillable property to allow mass assignment on [App\~].
- PHP Parse error: syntax error, unexpected 'class' (T_CLASS), expecting identifier (T_STRING) or variable (T_VARIABLE) or '{' or '$' in ~
- Changing columns for table "~" requires Doctrine DBAL; install "doctrine/dbal"
- MethodNotAllowedHttpException No message
- Class 'Doctrine\DBAL\Driver\PDOMySql\Driver' not found
- production.ERROR: No application encryption key has been specified.
- Dotenv values containing spaces must be surrounded by quotes.
- Laravel \ Socialite \ Two \ InvalidStateException
- The page has expired due to inactivity. Please refresh and try again.
- Failed to clone https://github.com/symfony/thanks.git via https, ssh protocol
- Illegal offset type
- Cannot access protected property Illuminate\Http\Request::$...
- Emitted value instead of an instance of Error
- 画像保存時にInternal Server Error
- Failed to authenticate on SMTP server with username ...
- PostTooLargeException
- Database hosts array is empty.
- Invalid request (Unsupported SSL request)
- does not comply with psr-4 autoloading standard. Skipping.
- MySQLのSTR_TO_DATE関数を使用するとnullが返却される問題
人気ページ
- 1 Eclipseで「サーバーに追加または除去できるリソースがありません。」の原因と対処法
- 2 tomcat の起動 / 停止ログと catalina.log・catalina.out の違い
- 3 JavaScript base URL 取得方法|window.location.origin と SSR/Node.js 対応
- 4 YouTube Data API v3 エラー一覧|403/400/404 の主要原因と切り分け
- 5 Spring Frameworkのアノテーション一覧
- 6 Laravel エラー一覧|500/Blade/DB 接続/ルーティングの代表エラー
- 7 3Dグラフィックスとは|モデリング/レンダリング/主要ソフトウェア (Blender / Maya)
- 8 【Spring】@Valueアノテーションとは
- 9 CATALINA_HOME の確認方法 (Linux / Mac)
- 10 【Spring】@Autowiredアノテーションとは
最近更新/作成されたページ
- Laravel キャッシュクリア完全ガイド(cache:clear / config:clear / 2026-05-18 07:42:07
- プロジェクトの作成と削除 2026-05-18 07:42:07
- インストール直後にNetbeansが反応しない 2026-05-18 07:42:07
- 動画やチャンネルの検索 2026-05-18 07:42:07
- APIキー取得方法 2026-05-18 07:42:07
- チャンネル情報の取得 2026-05-18 07:42:07
- API 入門 — Web API(REST / GraphQL / gRPC / 2026-05-18 07:42:07
- インストール(eclipseプラグイン) 2026-05-18 07:42:07
- Laravel「Dotenv values containing spaces must be surrounded 2026-05-18 07:42:07
- エラー一覧 2026-05-18 07:42:07
- curl: (51) SSL: certificate subject name '~' does not match 2026-05-18 07:42:07
- インストール方法(Windows版) 2026-05-18 07:42:07
- JSONから配列に変換 2026-05-18 07:42:07
- 処理を一定時間待つ 2026-05-18 07:42:07
- A non well formed numeric value encountered 2026-05-18 07:42:07
コメントを削除してもよろしいでしょうか?