7

有没有人遇到这个问题... PHP array index 同时存在 int 和 string, 这个时候 0 == 任...

 3 years ago
source link: https://www.v2ex.com/t/798784
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

V2EX  ›  PHP

有没有人遇到这个问题... PHP array index 同时存在 int 和 string, 这个时候 0 == 任何字符串, 这是为什么?

  JoeBreeze · 1 天前 · 732 次点击

已知解决: =====

<?php
$arr = [
    'hello'  => 'world',
    'world' => 'hello',
    'what'  => 'the hell',
    '0'        => 'zero in string key',
    0         => 'zero in int key',
    1         => 'one in int key',
];

var_dump($arr);

foreach($arr as $k => $v){
    echo '-------------------'.PHP_EOL;
    echo ($k=='hello'? '√': 'X').' | '.$k .'==hello'.PHP_EOL;
    echo ($k=='world'? '√': 'X').' | '.$k .'==world'.PHP_EOL;
    echo ($k=='what'? '√': 'X').' | '.$k .'==what'.PHP_EOL;
    echo ($k=='0'? '√': 'X').' | '.$k .'==0'.PHP_EOL;
    echo ($k=='1'? '√': 'X').' | '.$k .'==1'.PHP_EOL;
}
array(5) {
  ["hello"]=>
  string(5) "world"
  ["world"]=>
  string(5) "hello"
  ["what"]=>
  string(8) "the hell"
  [0]=>
  string(15) "zero in int key"
  [1]=>
  string(14) "one in int key"
}
-------------------
√ | hello==hello
X | hello==world
X | hello==what
X | hello==0
X | hello==1
-------------------
X | world==hello
√ | world==world
X | world==what
X | world==0
X | world==1
-------------------
X | what==hello
X | what==world
√ | what==what
X | what==0
X | what==1
-------------------
√ | 0==hello
√ | 0==world
√ | 0==what
√ | 0==0
X | 0==1
-------------------
X | 1==hello
X | 1==world
X | 1==what
X | 1==0
√ | 1==1

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK