php數組怪事,求解答

來源:互聯網
上載者:User
class dd {    const d = 'd';    public function test() {        $arr = array();        $arr['d'] = 'test';        $arr2 = array(                'a' => '1',                'b' => '2',                'c' => '3',                'd' => '4'            );        foreach ($arr2 as $key=>$value) {            if (in_array($key,array('a','b','c')) || isset($arr['self::d'])) {                echo $key;            }        }    }}$dd = new dd();$dd->test();

輸出:

abc

為什麼不是輸出abcd

========補充一下========其實我真正想問的是,單引號不是不解析嗎?為什麼echo $arr['self::d']。提示的是未定義'self::d'這個常量?提示是為定義'self::d'可不是'd'哦。

=======我是分割線========

回複內容:

class dd {    const d = 'd';    public function test() {        $arr = array();        $arr['d'] = 'test';        $arr2 = array(                'a' => '1',                'b' => '2',                'c' => '3',                'd' => '4'            );        foreach ($arr2 as $key=>$value) {            if (in_array($key,array('a','b','c')) || isset($arr['self::d'])) {                echo $key;            }        }    }}$dd = new dd();$dd->test();

輸出:

abc

為什麼不是輸出abcd

========補充一下========其實我真正想問的是,單引號不是不解析嗎?為什麼echo $arr['self::d']。提示的是未定義'self::d'這個常量?提示是為定義'self::d'可不是'd'哦。

=======我是分割線========

雖然已經有了不少回答了我還是說一下我的看法 ...

關於為什麼不是輸出 abcd 樓上說了很多我就不贅述了 ... 我單單說一下你遇到的這個錯誤 ...

事實上 ... 這是一個 非常詭異 的錯誤 ... 在我的知識範圍內這是不可能出現的 ...

首先 ... 一個類常量在任何情況下都不會被自動轉化為字串 ...

其次 ... 調用不存在的類常量只會引發 Warning 或者 Fatal ... 任何情況下都不會只是一個 Notice ...

最直白的證明 ... 我寫了一段測試代碼 ... 在相容箱裡跑了一下 ... 代碼和運行報告如下 ...

bar();------------------------------------------------------------------------php version 5.5.0beta4    Notice: Undefined index: self::xx in cevin on line 5    Fatal error: Undefined class constant 'xx' in cevin on line 6php version 5.5.0beta3    Notice: Undefined index: self::xx in cevin on line 5    Fatal error: Undefined class constant 'xx' in cevin on line 6php version 5.5.0beta2    Notice: Undefined index: self::xx in cevin on line 5    Fatal error: Undefined class constant 'xx' in cevin on line 6php version 5.5.0beta1    Notice: Undefined index: self::xx in cevin on line 5    Fatal error: Undefined class constant 'xx' in cevin on line 6php version 5.5.0alpha6    Notice: Undefined index: self::xx in cevin on line 5    Fatal error: Undefined class constant 'xx' in cevin on line 6php version 5.5.0alpha5    Notice: Undefined index: self::xx in cevin on line 5    Fatal error: Undefined class constant 'xx' in cevin on line 6php version 5.5.0alpha4    Notice: Undefined index: self::xx in cevin on line 5    Fatal error: Undefined class constant 'xx' in cevin on line 6php version 5.5.0alpha3    Notice: Undefined index: self::xx in cevin on line 5    Fatal error: Undefined class constant 'xx' in cevin on line 6php version 5.5.0alpha2    Notice: Undefined index: self::xx in cevin on line 5    Fatal error: Undefined class constant 'xx' in cevin on line 6php version 5.5.0alpha1    Notice: Undefined index: self::xx in cevin on line 5    Fatal error: Undefined class constant 'xx' in cevin on line 6php version 5.4.14    Notice: Undefined index: self::xx in cevin on line 5    Fatal error: Undefined class constant 'xx' in cevin on line 6php version 5.4.12    Notice: Undefined index: self::xx in cevin on line 5    Fatal error: Undefined class constant 'xx' in cevin on line 6php version 5.4.11    Notice: Undefined index: self::xx in cevin on line 5    Fatal error: Undefined class constant 'xx' in cevin on line 6php version 5.4.10    Notice: Undefined index: self::xx in cevin on line 5    Fatal error: Undefined class constant 'xx' in cevin on line 6php version 5.4.9    Notice: Undefined index: self::xx in cevin on line 5    Fatal error: Undefined class constant 'xx' in cevin on line 6php version 5.4.8    Notice: Undefined index: self::xx in cevin on line 5    Fatal error: Undefined class constant 'xx' in cevin on line 6php version 5.4.7    Notice: Undefined index: self::xx in cevin on line 5    Fatal error: Undefined class constant 'xx' in cevin on line 6php version 5.4.6    Notice: Undefined index: self::xx in cevin on line 5    Fatal error: Undefined class constant 'xx' in cevin on line 6php version 5.4.5    Notice: Undefined index: self::xx in cevin on line 5    Fatal error: Undefined class constant 'xx' in cevin on line 6php version 5.4.4    Notice: Undefined index: self::xx in cevin on line 5    Fatal error: Undefined class constant 'xx' in cevin on line 6php version 5.4.3    Notice: Undefined index: self::xx in cevin on line 5    Fatal error: Undefined class constant 'xx' in cevin on line 6php version 5.4.2    Notice: Undefined index: self::xx in cevin on line 5    Fatal error: Undefined class constant 'xx' in cevin on line 6php version 5.4.1    Notice: Undefined index: self::xx in cevin on line 5    Fatal error: Undefined class constant 'xx' in cevin on line 6php version 5.4.0    Notice: Undefined index: self::xx in cevin on line 5    Fatal error: Undefined class constant 'xx' in cevin on line 6php version 5.3.24    Notice: Undefined index: self::xx in cevin on line 5    Fatal error: Undefined class constant 'xx' in cevin on line 6php version 5.3.22    Notice: Undefined index: self::xx in cevin on line 5    Fatal error: Undefined class constant 'xx' in cevin on line 6php version 5.3.21    Notice: Undefined index: self::xx in cevin on line 5    Fatal error: Undefined class constant 'xx' in cevin on line 6php version 5.3.20    Notice: Undefined index: self::xx in cevin on line 5    Fatal error: Undefined class constant 'xx' in cevin on line 6php version 5.3.19    Notice: Undefined index: self::xx in cevin on line 5    Fatal error: Undefined class constant 'xx' in cevin on line 6php version 5.3.18    Notice: Undefined index: self::xx in cevin on line 5    Fatal error: Undefined class constant 'xx' in cevin on line 6php version 5.3.17    Notice: Undefined index: self::xx in cevin on line 5    Fatal error: Undefined class constant 'xx' in cevin on line 6php version 5.3.16    Notice: Undefined index: self::xx in cevin on line 5    Fatal error: Undefined class constant 'xx' in cevin on line 6php version 5.3.15    Notice: Undefined index: self::xx in cevin on line 5    Fatal error: Undefined class constant 'xx' in cevin on line 6php version 5.3.14    Notice: Undefined index: self::xx in cevin on line 5    Fatal error: Undefined class constant 'xx' in cevin on line 6php version 5.3.13    Notice: Undefined index: self::xx in cevin on line 5    Fatal error: Undefined class constant 'xx' in cevin on line 6php version 5.3.12    Notice: Undefined index: self::xx in cevin on line 5    Fatal error: Undefined class constant 'xx' in cevin on line 6php version 5.3.11    Notice: Undefined index: self::xx in cevin on line 5    Fatal error: Undefined class constant 'xx' in cevin on line 6php version 5.3.10    Notice: Undefined index: self::xx in cevin on line 5    Fatal error: Undefined class constant 'xx' in cevin on line 6php version 5.3.9    Notice: Undefined index: self::xx in cevin on line 5    Fatal error: Undefined class constant 'xx' in cevin on line 6php version 5.3.8    Notice: Undefined index: self::xx in cevin on line 5    Fatal error: Undefined class constant 'xx' in cevin on line 6php version 5.3.7    Notice: Undefined index: self::xx in cevin on line 5    Fatal error: Undefined class constant 'xx' in cevin on line 6php version 5.3.6    Notice: Undefined index: self::xx in cevin on line 5    Fatal error: Undefined class constant 'xx' in cevin on line 6php version 5.3.5    Notice: Undefined index: self::xx in cevin on line 5    Fatal error: Undefined class constant 'xx' in cevin on line 6php version 5.3.4    Notice: Undefined index: self::xx in cevin on line 5    Fatal error: Undefined class constant 'xx' in cevin on line 6php version 5.3.3    Notice: Undefined index: self::xx in cevin on line 5    Fatal error: Undefined class constant 'xx' in cevin on line 6php version 5.3.2    Notice: Undefined index: self::xx in cevin on line 5    Fatal error: Undefined class constant 'xx' in cevin on line 6php version 5.3.1    Notice: Undefined index: self::xx in cevin on line 5    Fatal error: Undefined class constant 'xx' in cevin on line 6php version 5.3.0    Notice: Undefined index: self::xx in cevin on line 5    Fatal error: Undefined class constant 'xx' in cevin on line 6php version 5.2.17    Notice: Undefined index: self::xx in cevin on line 5     Fatal error: Undefined class constant 'xx' in cevin on line 6php version 5.2.16    Notice: Undefined index: self::xx in cevin on line 5     Fatal error: Undefined class constant 'xx' in cevin on line 6php version 5.2.15    Notice: Undefined index: self::xx in cevin on line 5     Fatal error: Undefined class constant 'xx' in cevin on line 6php version 5.2.14    Notice: Undefined index: self::xx in cevin on line 5     Fatal error: Undefined class constant 'xx' in cevin on line 6php version 5.2.13    Notice: Undefined index: self::xx in cevin on line 5     Fatal error: Undefined class constant 'xx' in cevin on line 6php version 5.2.12    Notice: Undefined index: self::xx in cevin on line 5     Fatal error: Undefined class constant 'xx' in cevin on line 6php version 5.2.11    Notice: Undefined index: self::xx in cevin on line 5     Fatal error: Undefined class constant 'xx' in cevin on line 6php version 5.2.10    Notice: Undefined index: self::xx in cevin on line 5     Fatal error: Undefined class constant 'xx' in cevin on line 6php version 5.2.9    Notice: Undefined index: self::xx in cevin on line 5     Fatal error: Undefined class constant 'xx' in cevin on line 6php version 5.2.8    Notice: Undefined index: self::xx in cevin on line 5     Fatal error: Undefined class constant 'xx' in cevin on line 6php version 5.2.7    Notice: Undefined index: self::xx in cevin on line 5     Fatal error: Undefined class constant 'xx' in cevin on line 6php version 5.2.6    Notice: Undefined index: self::xx in cevin on line 5     Fatal error: Undefined class constant 'xx' in cevin on line 6php version 5.2.5    Notice: Undefined index: self::xx in cevin on line 5     Fatal error: Undefined class constant 'xx' in cevin on line 6php version 5.2.4    Notice: Undefined index: self::xx in cevin on line 5     Fatal error: Undefined class constant 'xx' in cevin on line 6php version 5.2.3    Notice: Undefined index: self::xx in cevin on line 5     Fatal error: Undefined class constant 'xx' in cevin on line 6php version 5.2.2    Notice: Undefined index: self::xx in cevin on line 5     Fatal error: Undefined class constant 'xx' in cevin on line 6php version 5.2.1    Notice: Undefined index: self::xx in cevin on line 5     Fatal error: Undefined class constant 'xx' in cevin on line 6php version 5.2.0    Notice: Undefined index: self::xx in cevin on line 5     Fatal error: Undefined class constant 'xx' in cevin on line 6php version 5.1.6    Notice: Undefined index: self::xx in cevin on line 5     Fatal error: Undefined class constant 'xx' in cevin on line 6php version 5.1.5    Notice: Undefined index: self::xx in cevin on line 5     Fatal error: Undefined class constant 'xx' in cevin on line 6php version 5.1.4    Notice: Undefined index: self::xx in cevin on line 5     Fatal error: Undefined class constant 'xx' in cevin on line 6php version 5.1.3    Notice: Undefined index: self::xx in cevin on line 5     Fatal error: Undefined class constant 'xx' in cevin on line 6php version 5.1.2    Notice: Undefined index: self::xx in cevin on line 5     Fatal error: Undefined class constant 'xx' in cevin on line 6php version 5.1.1    Notice: Undefined index: self::xx in cevin on line 5     Fatal error: Undefined class constant 'xx' in cevin on line 6php version 5.1.0    Notice: Undefined index: self::xx in cevin on line 5     Fatal error: Undefined class constant 'xx' in cevin on line 6php version 5.0.5    Notice: Undefined index: self::xx in cevin on line 5     Fatal error: Undefined class constant 'xx' in cevin on line 6php version 5.0.4    Notice: Undefined index: self::xx in cevin on line 5     Fatal error: Undefined class constant 'xx' in cevin on line 6php version 5.0.3    Notice: Undefined index: self::xx in cevin on line 5     Fatal error: Undefined class constant 'xx' in cevin on line 6php version 5.0.2    Notice: Undefined index: self::xx in cevin on line 5     Fatal error: Undefined class constant 'xx' in cevin on line 6php version 5.0.1    Notice: Undefined index: self::xx in cevin on line 5     Fatal error: Undefined class constant 'xx' in cevin on line 6php version 5.0.0    Notice: Undefined index: self::xx in cevin on line 5     Fatal error: Undefined class constant 'xx' in cevin on line 6php version 4.4.9    Parse error: syntax error, unexpected ']', expecting '(' in cevin on line 6php version 4.4.8    Parse error: syntax error, unexpected ']', expecting '(' in cevin on line 6php version 4.4.7    Parse error: syntax error, unexpected ']', expecting '(' in cevin on line 6php version 4.4.6    Parse error: syntax error, unexpected ']', expecting '(' in cevin on line 6php version 4.4.5    Parse error: syntax error, unexpected ']', expecting '(' in cevin on line 6php version 4.4.4    Parse error: syntax error, unexpected ']', expecting '(' in cevin on line 6php version 4.4.3    Parse error: syntax error, unexpected ']', expecting '(' in cevin on line 6php version 4.4.2    Parse error: syntax error, unexpected ']', expecting '(' in cevin on line 6php version 4.4.1    Parse error: parse error, unexpected ']', expecting '(' in cevin on line 6php version 4.4.0    Parse error: parse error, unexpected ']', expecting '(' in cevin on line 6php version 4.3.11    Parse error: parse error, unexpected ']', expecting '(' in cevin on line 6php version 4.3.10    Parse error: parse error, unexpected ']', expecting '(' in cevin on line 6php version 4.3.9    Parse error: parse error, unexpected ']', expecting '(' in cevin on line 6php version 4.3.8    Parse error: parse error, unexpected ']', expecting '(' in cevin on line 6php version 4.3.7    Parse error: parse error, unexpected ']', expecting '(' in cevin on line 6php version 4.3.6    Parse error: parse error, unexpected ']', expecting '(' in cevin on line 6php version 4.3.5    Parse error: parse error, unexpected ']', expecting '(' in cevin on line 6php version 4.3.4    Parse error: parse error, expecting `'('' in cevin on line 6php version 4.3.3    Parse error: parse error, expecting `'('' in cevin on line 6php version 4.3.2    Parse error: parse error, expecting `'('' in cevin on line 6php version 4.3.1    Parse error: parse error, unexpected ']', expecting '(' in cevin on line 6php version 4.3.0    Parse error: parse error, unexpected ']', expecting '(' in cevin on line 6

獅屎勝於熊便 ... 通過這份報告基本可以斷定這是你的 php 環境的問題 ...

不用懷疑自己的知識 ... 在這件事情上你對單引號的理解是對的 ... 不要被誤導了 ...

以及說 ... 如果你用的是 AppServ 提供的那個 php 6.0.0-dev 版本的 php 的話 ... 儘早換了吧 ...

恩 ... 就是這樣啦 ...

你的代碼寫錯了 $arr['self::d'] 應該修改為 $arr[self::d]

朋友關鍵看這裡哦

in_array($key,array('a','b','c'))

這說明$key範圍只能是在a,b,c這三個範圍才能輸出$key

再說說判斷條件中的isset($arr['self::d'])

在php中雙引號中的變數是可以執行的,而單引號是不解析變數的

所以$arr['self::d']是不存在的如果你需要將self::d當作鍵的話需可以這麼做$arr[self::d]

所有最終你得到的結果是abc

一般php在用變數做鍵擷取數組值時,不需要加引號,如$arr[$key]
對於你給出的問題,我剛做了實驗
isset($arr['self::d'])換成isset($arr[self::d])即可

補充

針對你的補充的notice資訊,我也做了實驗,echo $arr['self::d']提示資訊如下:
Notice: Undefined index: self::d in D:\servers\phpweb\guai.php on line 17 abc
並沒有發現和你相同的報錯資訊,針對這個不同,你需要確認兩點:
1、是否是由於你給出的例子和你真實想要寫的代碼之間有哪些不同導致了這個問題。
2、你的php版本是否太低。(我用的版本是PHP Version 5.4.5

這個問題需要先解決了你的補充問題。

1.執行 echo $arr['self::d'] 給出的notice的是:

Notice: Undefined index: self::d

注意如果你把這翻譯成中文應該是未定義的索引,而非未定義的常量。單引號不解析這句話是對的,因此php會直接在數組 $arr 中尋找索引(或者說下標,看你的習慣了)為 self::d 的元素,然後未找到,因此給出notice

2.為什麼輸出的是 abc 而非abcd?看完了上面的一個回答者問題就很容易了,因為單引號不解析,而 $arr 中索引為 self::d 的元素不存在,因此 isset($arr['self::d']) 這一個運算式是為 false 的。修正的方法(我覺得也是你的本意)其他幾位已經回答了,把 $arr['self::d'] 改為 $arr[self::d] 。沒有了單引號,那 self::d 就會被解析執行,判斷的就是 $arr['d'] 了。

其實只要在你調用之前用var_dump來輸出一下變數的值,看看輸出結果是什麼就可以了。'self::d',"self::d"始終獲得的是本身的字串。

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.