목차
回复内容:
补充
백엔드 개발 PHP 튜토리얼 php数组怪事,求解答

php数组怪事,求解答

Jun 06, 2016 pm 08:50 PM
php

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();
로그인 후 복사
로그인 후 복사

输出:

<code>abc
</code>
로그인 후 복사
로그인 후 복사

为什么不是输出abcd

========补充一下======== 其实我真正想问的是,单引号不是不解析么?为什么echo $arr['self::d']。提示的是未定义'self::d'这个常量?提示是为定义'self::d'可不是'd'哦。

=======我是分割线======== php数组怪事,求解答

回复内容:

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();
로그인 후 복사
로그인 후 복사

输出:

<code>abc
</code>
로그인 후 복사
로그인 후 복사

为什么不是输出abcd

========补充一下======== 其实我真正想问的是,单引号不是不解析么?为什么echo $arr['self::d']。提示的是未定义'self::d'这个常量?提示是为定义'self::d'可不是'd'哦。

=======我是分割线======== php数组怪事,求解答

虽然已经有了不少回答了我还是说一下我的看法 ...

关于为什么不是输出 abcd 楼上说了很多我就不赘述了 ... 我单单说一下你遇到的这个错误 ...

事实上 ... 这是一个 非常诡异 的错误 ... 在我的知识范围内这是不可能出现的 ...

首先 ... 一个类常量在任何情况下都不会被自动转化为字符串 ...

其次 ... 调用不存在的类常量只会引发 Warning 或者 Fatal ... 任何情况下都不会只是一个 Notice ...

最直白的证明 ... 我写了一段测试代码 ... 在兼容箱里跑了一下 ... 代码和运行报告如下 ...

<code><?php
class foo {
    function bar() {
        $baz = array();
        echo $baz['self::xx'];  // line 5
        echo $baz[self::xx];    // line 6
    }
}

$foo = new foo;
$foo->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 6

php version 5.5.0beta3
    Notice: Undefined index: self::xx in cevin on line 5
    Fatal error: Undefined class constant 'xx' in cevin on line 6

php version 5.5.0beta2
    Notice: Undefined index: self::xx in cevin on line 5
    Fatal error: Undefined class constant 'xx' in cevin on line 6

php version 5.5.0beta1
    Notice: Undefined index: self::xx in cevin on line 5
    Fatal error: Undefined class constant 'xx' in cevin on line 6

php version 5.5.0alpha6
    Notice: Undefined index: self::xx in cevin on line 5
    Fatal error: Undefined class constant 'xx' in cevin on line 6

php version 5.5.0alpha5
    Notice: Undefined index: self::xx in cevin on line 5
    Fatal error: Undefined class constant 'xx' in cevin on line 6

php version 5.5.0alpha4
    Notice: Undefined index: self::xx in cevin on line 5
    Fatal error: Undefined class constant 'xx' in cevin on line 6

php version 5.5.0alpha3
    Notice: Undefined index: self::xx in cevin on line 5
    Fatal error: Undefined class constant 'xx' in cevin on line 6

php version 5.5.0alpha2
    Notice: Undefined index: self::xx in cevin on line 5
    Fatal error: Undefined class constant 'xx' in cevin on line 6

php version 5.5.0alpha1
    Notice: Undefined index: self::xx in cevin on line 5
    Fatal error: Undefined class constant 'xx' in cevin on line 6

php version 5.4.14
    Notice: Undefined index: self::xx in cevin on line 5
    Fatal error: Undefined class constant 'xx' in cevin on line 6

php version 5.4.12
    Notice: Undefined index: self::xx in cevin on line 5
    Fatal error: Undefined class constant 'xx' in cevin on line 6

php version 5.4.11
    Notice: Undefined index: self::xx in cevin on line 5
    Fatal error: Undefined class constant 'xx' in cevin on line 6

php version 5.4.10
    Notice: Undefined index: self::xx in cevin on line 5
    Fatal error: Undefined class constant 'xx' in cevin on line 6

php version 5.4.9
    Notice: Undefined index: self::xx in cevin on line 5
    Fatal error: Undefined class constant 'xx' in cevin on line 6

php version 5.4.8
    Notice: Undefined index: self::xx in cevin on line 5
    Fatal error: Undefined class constant 'xx' in cevin on line 6

php version 5.4.7
    Notice: Undefined index: self::xx in cevin on line 5
    Fatal error: Undefined class constant 'xx' in cevin on line 6

php version 5.4.6
    Notice: Undefined index: self::xx in cevin on line 5
    Fatal error: Undefined class constant 'xx' in cevin on line 6

php version 5.4.5
    Notice: Undefined index: self::xx in cevin on line 5
    Fatal error: Undefined class constant 'xx' in cevin on line 6

php version 5.4.4
    Notice: Undefined index: self::xx in cevin on line 5
    Fatal error: Undefined class constant 'xx' in cevin on line 6

php version 5.4.3
    Notice: Undefined index: self::xx in cevin on line 5
    Fatal error: Undefined class constant 'xx' in cevin on line 6

php version 5.4.2
    Notice: Undefined index: self::xx in cevin on line 5
    Fatal error: Undefined class constant 'xx' in cevin on line 6

php version 5.4.1
    Notice: Undefined index: self::xx in cevin on line 5
    Fatal error: Undefined class constant 'xx' in cevin on line 6

php version 5.4.0
    Notice: Undefined index: self::xx in cevin on line 5
    Fatal error: Undefined class constant 'xx' in cevin on line 6

php version 5.3.24
    Notice: Undefined index: self::xx in cevin on line 5
    Fatal error: Undefined class constant 'xx' in cevin on line 6

php version 5.3.22
    Notice: Undefined index: self::xx in cevin on line 5
    Fatal error: Undefined class constant 'xx' in cevin on line 6

php version 5.3.21
    Notice: Undefined index: self::xx in cevin on line 5
    Fatal error: Undefined class constant 'xx' in cevin on line 6

php version 5.3.20
    Notice: Undefined index: self::xx in cevin on line 5
    Fatal error: Undefined class constant 'xx' in cevin on line 6

php version 5.3.19
    Notice: Undefined index: self::xx in cevin on line 5
    Fatal error: Undefined class constant 'xx' in cevin on line 6

php version 5.3.18
    Notice: Undefined index: self::xx in cevin on line 5
    Fatal error: Undefined class constant 'xx' in cevin on line 6

php version 5.3.17
    Notice: Undefined index: self::xx in cevin on line 5
    Fatal error: Undefined class constant 'xx' in cevin on line 6

php version 5.3.16
    Notice: Undefined index: self::xx in cevin on line 5
    Fatal error: Undefined class constant 'xx' in cevin on line 6

php version 5.3.15
    Notice: Undefined index: self::xx in cevin on line 5
    Fatal error: Undefined class constant 'xx' in cevin on line 6

php version 5.3.14
    Notice: Undefined index: self::xx in cevin on line 5
    Fatal error: Undefined class constant 'xx' in cevin on line 6

php version 5.3.13
    Notice: Undefined index: self::xx in cevin on line 5
    Fatal error: Undefined class constant 'xx' in cevin on line 6

php version 5.3.12
    Notice: Undefined index: self::xx in cevin on line 5
    Fatal error: Undefined class constant 'xx' in cevin on line 6

php version 5.3.11
    Notice: Undefined index: self::xx in cevin on line 5
    Fatal error: Undefined class constant 'xx' in cevin on line 6

php version 5.3.10
    Notice: Undefined index: self::xx in cevin on line 5
    Fatal error: Undefined class constant 'xx' in cevin on line 6

php version 5.3.9
    Notice: Undefined index: self::xx in cevin on line 5
    Fatal error: Undefined class constant 'xx' in cevin on line 6

php version 5.3.8
    Notice: Undefined index: self::xx in cevin on line 5
    Fatal error: Undefined class constant 'xx' in cevin on line 6

php version 5.3.7
    Notice: Undefined index: self::xx in cevin on line 5
    Fatal error: Undefined class constant 'xx' in cevin on line 6

php version 5.3.6
    Notice: Undefined index: self::xx in cevin on line 5
    Fatal error: Undefined class constant 'xx' in cevin on line 6

php version 5.3.5
    Notice: Undefined index: self::xx in cevin on line 5
    Fatal error: Undefined class constant 'xx' in cevin on line 6

php version 5.3.4
    Notice: Undefined index: self::xx in cevin on line 5
    Fatal error: Undefined class constant 'xx' in cevin on line 6

php version 5.3.3
    Notice: Undefined index: self::xx in cevin on line 5
    Fatal error: Undefined class constant 'xx' in cevin on line 6

php version 5.3.2
    Notice: Undefined index: self::xx in cevin on line 5
    Fatal error: Undefined class constant 'xx' in cevin on line 6

php version 5.3.1
    Notice: Undefined index: self::xx in cevin on line 5
    Fatal error: Undefined class constant 'xx' in cevin on line 6

php version 5.3.0
    Notice: Undefined index: self::xx in cevin on line 5
    Fatal error: Undefined class constant 'xx' in cevin on line 6

php version 5.2.17
    Notice: Undefined index: self::xx in cevin on line 5 
    Fatal error: Undefined class constant 'xx' in cevin on line 6

php version 5.2.16
    Notice: Undefined index: self::xx in cevin on line 5 
    Fatal error: Undefined class constant 'xx' in cevin on line 6

php version 5.2.15
    Notice: Undefined index: self::xx in cevin on line 5 
    Fatal error: Undefined class constant 'xx' in cevin on line 6

php version 5.2.14
    Notice: Undefined index: self::xx in cevin on line 5 
    Fatal error: Undefined class constant 'xx' in cevin on line 6

php version 5.2.13
    Notice: Undefined index: self::xx in cevin on line 5 
    Fatal error: Undefined class constant 'xx' in cevin on line 6

php version 5.2.12
    Notice: Undefined index: self::xx in cevin on line 5 
    Fatal error: Undefined class constant 'xx' in cevin on line 6

php version 5.2.11
    Notice: Undefined index: self::xx in cevin on line 5 
    Fatal error: Undefined class constant 'xx' in cevin on line 6

php version 5.2.10
    Notice: Undefined index: self::xx in cevin on line 5 
    Fatal error: Undefined class constant 'xx' in cevin on line 6

php version 5.2.9
    Notice: Undefined index: self::xx in cevin on line 5 
    Fatal error: Undefined class constant 'xx' in cevin on line 6

php version 5.2.8
    Notice: Undefined index: self::xx in cevin on line 5 
    Fatal error: Undefined class constant 'xx' in cevin on line 6

php version 5.2.7
    Notice: Undefined index: self::xx in cevin on line 5 
    Fatal error: Undefined class constant 'xx' in cevin on line 6

php version 5.2.6
    Notice: Undefined index: self::xx in cevin on line 5 
    Fatal error: Undefined class constant 'xx' in cevin on line 6

php version 5.2.5
    Notice: Undefined index: self::xx in cevin on line 5 
    Fatal error: Undefined class constant 'xx' in cevin on line 6

php version 5.2.4
    Notice: Undefined index: self::xx in cevin on line 5 
    Fatal error: Undefined class constant 'xx' in cevin on line 6

php version 5.2.3
    Notice: Undefined index: self::xx in cevin on line 5 
    Fatal error: Undefined class constant 'xx' in cevin on line 6

php version 5.2.2
    Notice: Undefined index: self::xx in cevin on line 5 
    Fatal error: Undefined class constant 'xx' in cevin on line 6

php version 5.2.1
    Notice: Undefined index: self::xx in cevin on line 5 
    Fatal error: Undefined class constant 'xx' in cevin on line 6

php version 5.2.0
    Notice: Undefined index: self::xx in cevin on line 5 
    Fatal error: Undefined class constant 'xx' in cevin on line 6

php version 5.1.6
    Notice: Undefined index: self::xx in cevin on line 5 
    Fatal error: Undefined class constant 'xx' in cevin on line 6

php version 5.1.5
    Notice: Undefined index: self::xx in cevin on line 5 
    Fatal error: Undefined class constant 'xx' in cevin on line 6

php version 5.1.4
    Notice: Undefined index: self::xx in cevin on line 5 
    Fatal error: Undefined class constant 'xx' in cevin on line 6

php version 5.1.3
    Notice: Undefined index: self::xx in cevin on line 5 
    Fatal error: Undefined class constant 'xx' in cevin on line 6

php version 5.1.2
    Notice: Undefined index: self::xx in cevin on line 5 
    Fatal error: Undefined class constant 'xx' in cevin on line 6

php version 5.1.1
    Notice: Undefined index: self::xx in cevin on line 5 
    Fatal error: Undefined class constant 'xx' in cevin on line 6

php version 5.1.0
    Notice: Undefined index: self::xx in cevin on line 5 
    Fatal error: Undefined class constant 'xx' in cevin on line 6

php version 5.0.5
    Notice: Undefined index: self::xx in cevin on line 5 
    Fatal error: Undefined class constant 'xx' in cevin on line 6

php version 5.0.4
    Notice: Undefined index: self::xx in cevin on line 5 
    Fatal error: Undefined class constant 'xx' in cevin on line 6

php version 5.0.3
    Notice: Undefined index: self::xx in cevin on line 5 
    Fatal error: Undefined class constant 'xx' in cevin on line 6

php version 5.0.2
    Notice: Undefined index: self::xx in cevin on line 5 
    Fatal error: Undefined class constant 'xx' in cevin on line 6

php version 5.0.1
    Notice: Undefined index: self::xx in cevin on line 5 
    Fatal error: Undefined class constant 'xx' in cevin on line 6

php version 5.0.0
    Notice: Undefined index: self::xx in cevin on line 5 
    Fatal error: Undefined class constant 'xx' in cevin on line 6

php version 4.4.9
    Parse error: syntax error, unexpected ']', expecting '(' in cevin on line 6

php version 4.4.8
    Parse error: syntax error, unexpected ']', expecting '(' in cevin on line 6

php version 4.4.7
    Parse error: syntax error, unexpected ']', expecting '(' in cevin on line 6

php version 4.4.6
    Parse error: syntax error, unexpected ']', expecting '(' in cevin on line 6

php version 4.4.5
    Parse error: syntax error, unexpected ']', expecting '(' in cevin on line 6

php version 4.4.4
    Parse error: syntax error, unexpected ']', expecting '(' in cevin on line 6

php version 4.4.3
    Parse error: syntax error, unexpected ']', expecting '(' in cevin on line 6

php version 4.4.2
    Parse error: syntax error, unexpected ']', expecting '(' in cevin on line 6

php version 4.4.1
    Parse error: parse error, unexpected ']', expecting '(' in cevin on line 6

php version 4.4.0
    Parse error: parse error, unexpected ']', expecting '(' in cevin on line 6

php version 4.3.11
    Parse error: parse error, unexpected ']', expecting '(' in cevin on line 6

php version 4.3.10
    Parse error: parse error, unexpected ']', expecting '(' in cevin on line 6

php version 4.3.9
    Parse error: parse error, unexpected ']', expecting '(' in cevin on line 6

php version 4.3.8
    Parse error: parse error, unexpected ']', expecting '(' in cevin on line 6

php version 4.3.7
    Parse error: parse error, unexpected ']', expecting '(' in cevin on line 6

php version 4.3.6
    Parse error: parse error, unexpected ']', expecting '(' in cevin on line 6

php version 4.3.5
    Parse error: parse error, unexpected ']', expecting '(' in cevin on line 6

php version 4.3.4
    Parse error: parse error, expecting `'('' in cevin on line 6

php version 4.3.3
    Parse error: parse error, expecting `'('' in cevin on line 6

php version 4.3.2
    Parse error: parse error, expecting `'('' in cevin on line 6

php version 4.3.1
    Parse error: parse error, unexpected ']', expecting '(' in cevin on line 6

php version 4.3.0
    Parse error: parse error, unexpected ']', expecting '(' in cevin on line 6
</code>
로그인 후 복사

狮屎胜于熊便 ... 通过这份报告基本可以断定这是你的 php 环境的问题 ...

不用怀疑自己的知识 ... 在这件事情上你对单引号的理解是对的 ... 不要被误导了 ...

以及说 ... 如果你用的是 AppServ 提供的那个 php 6.0.0-dev 版本的 php 的话 ... 尽早换了吧 ...

恩 ... 就是这样啦 ...

你的代码写错了 $arr['self::d'] 应该修改为 $arr[self::d]

朋友关键看这里哦

<code>in_array($key,array('a','b','c'))
</code>
로그인 후 복사

这说明$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"始终获得的是本身的字符串。

본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.

핫 AI 도구

Undresser.AI Undress

Undresser.AI Undress

사실적인 누드 사진을 만들기 위한 AI 기반 앱

AI Clothes Remover

AI Clothes Remover

사진에서 옷을 제거하는 온라인 AI 도구입니다.

Undress AI Tool

Undress AI Tool

무료로 이미지를 벗다

Clothoff.io

Clothoff.io

AI 옷 제거제

AI Hentai Generator

AI Hentai Generator

AI Hentai를 무료로 생성하십시오.

뜨거운 도구

메모장++7.3.1

메모장++7.3.1

사용하기 쉬운 무료 코드 편집기

SublimeText3 중국어 버전

SublimeText3 중국어 버전

중국어 버전, 사용하기 매우 쉽습니다.

스튜디오 13.0.1 보내기

스튜디오 13.0.1 보내기

강력한 PHP 통합 개발 환경

드림위버 CS6

드림위버 CS6

시각적 웹 개발 도구

SublimeText3 Mac 버전

SublimeText3 Mac 버전

신 수준의 코드 편집 소프트웨어(SublimeText3)

CakePHP 프로젝트 구성 CakePHP 프로젝트 구성 Sep 10, 2024 pm 05:25 PM

이번 장에서는 CakePHP의 환경 변수, 일반 구성, 데이터베이스 구성, 이메일 구성에 대해 알아봅니다.

Ubuntu 및 Debian용 PHP 8.4 설치 및 업그레이드 가이드 Ubuntu 및 Debian용 PHP 8.4 설치 및 업그레이드 가이드 Dec 24, 2024 pm 04:42 PM

PHP 8.4는 상당한 양의 기능 중단 및 제거를 통해 몇 가지 새로운 기능, 보안 개선 및 성능 개선을 제공합니다. 이 가이드에서는 Ubuntu, Debian 또는 해당 파생 제품에서 PHP 8.4를 설치하거나 PHP 8.4로 업그레이드하는 방법을 설명합니다.

CakePHP 날짜 및 시간 CakePHP 날짜 및 시간 Sep 10, 2024 pm 05:27 PM

cakephp4에서 날짜와 시간을 다루기 위해 사용 가능한 FrozenTime 클래스를 활용하겠습니다.

CakePHP 파일 업로드 CakePHP 파일 업로드 Sep 10, 2024 pm 05:27 PM

파일 업로드 작업을 위해 양식 도우미를 사용할 것입니다. 다음은 파일 업로드의 예입니다.

CakePHP 라우팅 CakePHP 라우팅 Sep 10, 2024 pm 05:25 PM

이번 장에서는 라우팅과 관련된 다음과 같은 주제를 학습하겠습니다.

CakePHP 토론 CakePHP 토론 Sep 10, 2024 pm 05:28 PM

CakePHP는 PHP용 오픈 소스 프레임워크입니다. 이는 애플리케이션을 훨씬 쉽게 개발, 배포 및 유지 관리할 수 있도록 하기 위한 것입니다. CakePHP는 강력하고 이해하기 쉬운 MVC와 유사한 아키텍처를 기반으로 합니다. 모델, 뷰 및 컨트롤러 gu

PHP 개발을 위해 Visual Studio Code(VS Code)를 설정하는 방법 PHP 개발을 위해 Visual Studio Code(VS Code)를 설정하는 방법 Dec 20, 2024 am 11:31 AM

VS Code라고도 알려진 Visual Studio Code는 모든 주요 운영 체제에서 사용할 수 있는 무료 소스 코드 편집기 또는 통합 개발 환경(IDE)입니다. 다양한 프로그래밍 언어에 대한 대규모 확장 모음을 통해 VS Code는

CakePHP 유효성 검사기 만들기 CakePHP 유효성 검사기 만들기 Sep 10, 2024 pm 05:26 PM

컨트롤러에 다음 두 줄을 추가하면 유효성 검사기를 만들 수 있습니다.

See all articles