2010-08-29 78 views
0

我在PHP中一遍又一遍地面對這些代碼,這是如何工作在PHP?這種類型的數組如何在PHP中運行?

$data[$row['id']] 

$options['data']=$row[0]; 
+2

我不明白,這些數組的哪個方面是你的問題?使用字符串和數字作爲數組鍵還是嵌套? – 2010-08-29 21:12:14

回答

0
// Let's initialize some variables. 
$row = array(); 
$row[0] = 999; 
$row['id'] = 6; 
// $row is now equal to array(0 => 999, 'id' => 6) 

$data = array(2, 3, 5, 7, 11, 13, 17, 19); 
// $data[0] is 2; $data[1] is 3. 

// At this point, 
$data[$row['id']] == // really means... 
$data[6] ==   // which equals... 
17; 

$options['data'] = $row[0]; 
$options[] = 66; 
$options[44] = 77; 
$options[] = 88; 

// $options is now equal to array('data' => 999, 0 => 66, 44 => 77, 55 => 88) 

數組只是鍵 - 值對。使用$array[] =語法告訴PHP爲新元素分配一個鍵。 PHP採用最高的整數密鑰並添加一個來獲取新密鑰。

0

數組在PHP更像哈希,在那裏他們可以有基於字符串的索引。我假設$row['id']包含一個數字或字符串,然後用於使用該鍵訪問值。

相關問題