0

请解释一下:)

如果我跑

$custId = getExternalId();
echo $custId . "\n"; // Prints foobar_16262499_1
$serial = '';

$custId = explode('_', $custId);
var_dump($custId);
$custId = $custId[1];
$serial = $custId[2];

die("custId: $custId serial: $serial\n");

我明白了

custId: 16262499 serial: 2

这是不正确的。序列号应该是 1。但是如果我将分配顺序更改为

$custId = getExternalId();
echo $custId . "\n"; // Prints foobar_16262499_1
$serial = '';

$custId = explode('_', $custId);
var_dump($custId);
$serial = $custId[2];   // Change order here!!!
$custId = $custId[1];

die("custId: $custId serial: $serial\n");

它有效并给了我

custId: 16262499 serial: 1

为什么?

在这两种情况下,数组的 var_dump 都会产生相同的输出:

array(3) {
  [0]=>
  string(4) "foobar"
  [1]=>
  string(8) "16262499"
  [2]=>
  string(1) "1"
}

我正在运行 PHP/5.3.3 ZendServer

SMACKS HEAD ...我怎么能错过明显的:)...

4

2 回答 2

2

你覆盖

$custId 

当你写下这一行

$custId = $custId[1];

所以在那之后你会得到一些你没想到的东西

$serial = $custId[2];

所以这样做

list($custId,$serial) = array($custId[1],$custId[2]); 
于 2011-01-24T12:51:21.223 回答
0
1.    $custId = $custId[1];
2.    $serial = $custId[2]; // **

** 这实际上意味着,原始 getExternalId() 的 ($custId[1])[2];

因为第 1 行之后的变量 $custId 不再是

$custId = getExternalId();

而是它的第二个元素(索引 [1])。

您可以通过再转储一次来添加调试

$custId = explode('_', $custId);
var_dump($custId);
$custId = $custId[1];
var_dump($custId);
$serial = $custId[2];
于 2011-01-24T12:52:38.633 回答