为什么同样的输入给同样的函数,出来结果不一样?
php
大家帮忙看一下,两次分别给函数输入数组,两次输入的数组用print_r打印出来看着是一样的,但是经过一个简单的函数之后出来结果就不一样了。看下面的代码和输出:function getKey($arr) { return $arrKey = array_keys($arr, 'aa');}$testArr = array('0'=>'XX','aa'=>'XX','1'=>'YY','bb'=>'YY','2'=>'ZZ','cc'=>'ZZ');$array1 = array_keys($testArr);$array2 = array('0', 'aa', '1', 'bb', '2','cc');print_r($array1);echo '<br>';print_r($array2);echo '<br>';print_r(getKey($array1));echo '<br>';print_r(getKey($array2));
我这里的输出结果是这样的:
Array ( [0] => 0 [1] => aa [2] => 1 [3] => bb [4] => 2 [5] => cc )
Array ( [0] => 0 [1] => aa [2] => 1 [3] => bb [4] => 2 [5] => cc )
Array ( [0] => 0 [1] => 1 )
Array ( [0] => 1 )
前面两个一样,后面两个却不一样,我怀疑可能是输入的两个数组用print_r打印出来一样,但其实内部有东西是不一样的,但是怎样能看到这种不同呢?然后到底有什么不同呢?
希望懂的不吝赐教,先谢过了。
回复讨论(解决方案)
return $arrKey = array_keys($arr, 'aa',true);
会得到相同的结果
array_keys 中 0 和 '0' 是有区别的
这一点你可以用 var_dump 观察到
array_keys($arr, 'aa')
是返回值为 aa 的元素的键
当值为 0 时, 'aa' == 0 因为一边是数值,所以 aa 会转换为 0 进行比较,表达式成立
当值为 '0' 时, 'aa' == '0' 因为两边是字符串,无需转换,表达式不成立成立
<?php function getKey($arr) { return $arrKey = array_keys($arr, 'aa'); } $testArr = array('0'=>'XX','aa'=>'XX','1'=>'YY','bb'=>'YY','2'=>'ZZ','cc'=>'ZZ'); $array1 = array_keys($testArr); $array2 = array('0', 'aa', '1', 'bb', '2','cc'); var_dump($array1); echo '<br>'; var_dump($array2); echo '<br>'; print_r(getKey($array1)); echo '<br>'; print_r(getKey($array2)); echo '<br>'; $arr = array(0,'xx',0,'bb'); print_r(array_keys($arr, 'aa'));?>
得到的结果:
array
0 => int 0
1 => string 'aa' (length=2)
2 => int 1
3 => string 'bb' (length=2)
4 => int 2
5 => string 'cc' (length=2)
array
0 => string '0' (length=1)
1 => string 'aa' (length=2)
2 => string '1' (length=1)
3 => string 'bb' (length=2)
4 => string '2' (length=1)
5 => string 'cc' (length=2)
Array ( [0] => 0 [1] => 1 )
Array ( [0] => 1 )
Array ( [0] => 0 [1] => 2 )
看出什么了吗 array_keys(array,value,strict) php是弱语言数字0被当成某个字符处理了 所以strict要指定为true
如果 strict 参数指定为 true,则 PHP 会使用全等比较 (===) 来严格检查键值的数据类型。
感谢microlab2009和xuzuning的详细指导,这下很明白了。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Alipay PHP...

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

How to automatically set the permissions of unixsocket after the system restarts. Every time the system restarts, we need to execute the following command to modify the permissions of unixsocket: sudo...

Article discusses late static binding (LSB) in PHP, introduced in PHP 5.3, allowing runtime resolution of static method calls for more flexible inheritance.Main issue: LSB vs. traditional polymorphism; LSB's practical applications and potential perfo

How to debug CLI mode in PHPStorm? When developing with PHPStorm, sometimes we need to debug PHP in command line interface (CLI) mode...

Sending JSON data using PHP's cURL library In PHP development, it is often necessary to interact with external APIs. One of the common ways is to use cURL library to send POST�...

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.
