我有一個數組,看起來像這樣,那我通過循環: 數組的名稱爲$測試非法失調誤差
Array
(
[ven1] => Array
(
[0] => Array
(
[0] => ven1-center1
[1] => ven1
[2] => Address
)
[1] => Array
(
[0] => ven1-center2
[1] => ven1
[2] => Address
)
[2] => Array
(
[0] => ven1-center3
[1] => ven1
[2] => Address
)
.
.
.
[ven2] => Array
(
[20] => Array
(
[0] => ven2-center1
[1] => ven2
[2] => Address
)
[21] => Array
(
[0] => ven2-center2
[1] => ven2
[2] => Address
)
[22] => Array
(
[0] => ven2-center3
[1] => ven2
[2] => Address
)
下面我有循環整個數組的PHP代碼,從最內層獲取[0]的所有值,並將它們存儲到名爲$ k的數組中。 當我運行下面的代碼時,我得到Notice: Undefined offset:
所有$ c的值。
for($c = 0; $c < 42; $c++){
foreach($test as $k => $v){
if($test[$k][$c][1] == $k){
$k = $test[$k][$c][0];
}
}
}
輸出在找的是
$ven1 = array("ven1-center1","ven1-center2","ven1-center3","ven1-center4")
$ven2 = array("ven1-center1","ven1-center2","ven1-center3","ven1-center4")
創建類似編號的變量是幾乎總是錯的。使用數組。 – Barmar
@Barmar你能解釋一下你的意思嗎? – hello
而不是'$ ven1'和'$ ven2',你應該有一個'$ ven'數組,其中'$ ven ['ven1'] = array(「ven1-center1」,「ven1-center2」,「ven1-中心3「,」ven1-center4「)和'$ ven ['ven2'] =陣列(」ven1-center1「,」ven1-center2「,」ven1-center3「,」ven1-center4「)。 – Barmar