2012-11-30 26 views
3

它已經一段時間,因爲我沒有任何PHP編程所以我想踢生鏽。關聯數組(2舞伴)

我想創建一個關聯數組結構。

[results] 
    [total] 
    [people] 
     [name] 
     [street] 
     [city] 
     [state] 
     [zip] 

Currently, I have this. 

$people = array('name' => '', 
       'street' => '', 
       'city' => '', 
       'state' => '', 
       'zip' =>); 

$results = array('total' => 10, --set dynamically 
       'people' => $people); 

所以在我的腦子裏,我希望做一個空多維數組,我將能夠在一個while循環來填充。

首先,問題是這個適當的形式?我覺得我很接近但不對。瞭解我在做什麼可能會有所幫助(如下所示)。

所以我作爲一個說我想填補這個在一個while循環,這基本上是我到目前爲止。到目前爲止,我一直無法工作。

$i = 0; 
while loop 
{ 
    $results['people'][i][name] = 'XxXxX' 
    $results['people'][i][street] = 'XxXxX' 
    $results['people'][i][city] = 'XxXxX' 
    $results['people'][i][state] = 'XxXxX' 
    $results['people'][i][zip] = 'XxXxX' 


%i++; 
} 

我已經嘗試過很多不同的組合,並且仍然無法把它弄清楚。如果它很重要,我想把這個數組作爲一個JSON對象發送回瀏覽器。

我不確定如果我的初始化是錯誤的,在循環中設置數組是錯誤的,或兩者。

+0

幾個指針,因爲你在這裏的[I]必須[$ i],名稱/街道等鍵需要用引號括起來,您的增量需求爲$我不是%1,而你的條件需要放在括號內。 – WebChemist

回答

1

PHP陣列需要單獨實例化,並就地。我不知道如何正確地描述它,但你的代碼看起來應該是這樣的:

$results = array(); 
$results['total'] = $somevalue; 
$results['people'] = array(); 

/*or: 
$results = array(
    'total' => $somevalue, 
    'people' => array() 
);*/ 

$i = 0; 
while($some_condition) { //or: for($i=0; $i<$something; $i++) { 
    $results['people'][$i] = array(); 
    $results['people'][$i]['name'] = 'XxXxX'; 
    $results['people'][$i]['street'] = 'XxXxX'; 
    $results['people'][$i]['city'] = 'XxXxX'; 
    $results['people'][$i]['state'] = 'XxXxX'; 
    $results['people'][$i]['zip'] = 'XxXxX'; 

    /*or: 
    $results['people'][$i] = array(
     'name' => 'XxXxX', 
     'street' => 'XxXxX', 
     'city' => 'XxXxX', 
     'state' => 'XxXxX', 
     'zip' => 'XxXxX', 
    );*/ 

    $i++; 
} 

記住,如果你使用的關聯數組,你需要用引號括密鑰字符串。同樣,你仍然可以使用整數索引來訪問關聯數組,你應該感覺如此傾向。

+0

非常感謝您的回覆。這正是我所期待的。 – complexenigma

+0

是啊,那種聽起來像是你從像C的語言,你大致看出機器的一個未來的「我需要的int X X Y X Z陣」,它砰地一聲撂下了這麼大的內存立即,這一切都準備就緒如果你需要更多,天堂會幫助你。從這個到PHP的怪異數組和動態類型的半易理解叢集是一個mindfck。而且PHP從這裏開始*下坡*:D – Sammitch

0

有我看到的幾個問題。首先是你有%i++而不是$i++。稍後,您參考i而不是$i。接下來是在你的while循環中,你正試圖訪問名稱,街道等ect而不使用引號(根據你的配置,這可能/不會顯示警告)。

嘗試使用這樣的:

$i = 0; 
while(NEED SOME CONDITION HERE) 
{ 
    $results['people'][$i] = array(); //Need to let PHP know this will be an array 
    $results['people'][$i]['name'] = 'XxXxX' 
    $results['people'][$i]['street'] = 'XxXxX' 
    $results['people'][$i]['city'] = 'XxXxX' 
    $results['people'][$i]['state'] = 'XxXxX' 
    $results['people'][$i]['zip'] = 'XxXxX' 


$i++; 
} 
0
$i = 0; 
while (true) 
{ 
    $results['people'][$i]['name'] = 'XxXxX' 
    $results['people'][$i]['street'] = 'XxXxX' 
    $results['people'][$i]['city'] = 'XxXxX' 
    $results['people'][$i]['state'] = 'XxXxX' 
    $results['people'][$i]['zip'] = 'XxXxX' 


$i++; 
} 
+0

這會導致無限循環,因爲沒有任何條件可能會導致真正的虛假或破壞以逃脫循環。事實上,如果是一個不變的話,將不可能將其價值重新分配到虛假! – WebChemist

0

首先而不是命名所有的按鍵和聲明空字符串,你可以只讓名稱的數組,並使用array_fill_keys將它們轉換爲鑰匙,讓他們全部默認值(除非你需要在一個循環中使用的append(.=)應該使用NULL,而不是''。 而不是while循環,我只想用一個for循環,但如果你喜歡你可以做一個while $i < 10與超過for

$people = array_fill_keys(array('name', 'street', 'city', 'state', 'zip'), ''); 

$results = array('total' => 10, 'people' => array()); 

for($i = 0; $i < $results['total']; $i++){ 
    $results['people'][$i]['name'] = 'XxXxX'; 
    $results['people'][$i]['street'] = 'XxXxX'; 
    $results['people'][$i]['city'] = 'XxXxX'; 
    $results['people'][$i]['state'] = 'XxXxX'; 
    $results['people'][$i]['zip'] = 'XxXxX'; 
}