0

如果我键入这样的内容,是否有任何工具可以运行 PHP 代码?

$myarray = array(
    'foo' => 'hello',
    'bar' => 'goodbye',
    'foo' => 'hello again' // <= need to pick up the duplicate key on this line
);

编辑:我想要这样的东西除了不是专有的。

4

1 回答 1

0

您正在覆盖您的数组键,因此 PHP 只会拾取键 'foo' 的最后一个条目

$arr = array(
    'foo' => 'hello',
    'bar' => 'goodbye',
    'foo' => 'hello again' // <= need to pick up the duplicate key on this line
);

print_r($arr);

返回:

数组( [foo] => 再次打招呼 [bar] => 再见)

但:

$arr = array(
    'foo' => 'hello',
    'bar' => 'goodbye',
    'foo3' => 'hello again' // <= need to pick up the duplicate key on this line
);

print_r($arr);

返回:

数组([foo] => hello [bar] => goodbye [foo3] => hello again)

甚至在 foreach 循环内(单步执行您的数组)

$arr = array(
    'foo' => 'hello',
    'bar' => 'goodbye',
    'foo' => 'hello again' // <= need to pick up the duplicate key on this line
);

foreach ($arr AS $Keys => $Value)
{
    echo $Keys;
    echo "<br>";
}

返回:

bar
foo

整体道德:

您的数组键正在被覆盖,因此 PHP 不会识别出重复的键。

于 2013-03-21T03:13:41.083 回答