配列のようなPHP文字列内の要素を参照する場合、角括弧[]を使用して[2]のような数字を使用して文字列から特定の文字を選択できます。ただし、["example"]のような文字列インデックスを使用する場合、常に[0]と同じ結果が返されます。
<?php
$str="Hello world.";
echo $str; // Echos "Hello world." as expected.
echo $str[2]; // Echos "l" as expected.
echo $str["example"]; // Echos "H", not expected.
$arr=array();
$arr["key"]="Test."
echo $arr["key"]; // Echos "Test." as expected.
echo $arr["invalid"]; // Gives an "undefined index" error as expected.
echo $arr["key"];
なぜ[0]と同じ結果が返されますか?
'$ strの[ "例"]'から '0を返します:あなたはそれを "9penguins" のような文字列を与えると、あなたがタイプジャグリング9.
PHPマニュアルを取得します$ str = "Hello world。"これは最初の文字 "H"を返します –
ここを参照してください理由:http://stackoverflow.com/questions/17193597/strings-as-arrays-in-php – mitkosoft
'$ str [ "example"] '=' $ str ["0"]のために例 'とhense hが受信される –