2015-04-15 65 views
4

我需要幫助,我的陣列結果有問題。表中的陣列排列

,我正在使用的代碼:

$presents = json_decode(fBGetDataStore('presents'), true); 

,並打印出結果:

 <?= print_r($presents) ?> 

顯示了這種結果:

Array ([0] => Array ([sender] => 100009016810227 [itemCode] => 0f8g [itemContext] => normal [extraData] =>) 
      [1] => Array ([sender] => 100009016810227 [itemCode] => 0fcm [itemContext] => normal [extraData] =>) 

但我想結果:

[1] Sender ID: 100009016810227 and Item Code is 0f8g 
[2] Sender ID: 100009016810227 and Item Code is 0fcm 
+1

爲什麼你想讓輸出鍵從1開始? –

+0

這是數字計數...或者它沒有必要計算結果.. 這種格式也是可以接受的 發件人ID:100009016810227和物品代碼是0f8g' –

回答

2

我照你說的@FatAdama

編碼:

foreach ($presents as $key=>$value) { echo 'Sender ID: '.$value['sender'].' and Item Code is '.$value['itemCode'].'<br>'; }

,並讓我的慾望導致^ _ ^:

Sender ID: 100009016810227 and Item Code is 0f8g Sender ID: 100009016810227 and Item Code is 0fcm

謝謝你的一切幫助。

2

你可以只是簡單的遍歷在foreach和建設所需的輸出到輸出數組,這樣

foreach ($presents as $key=>$value) { 
    $newKey = $key + 1; 
    $output[$newKey] = "Sender ID: {$value['sender']} and Item Code is {$value['itemCode']}"; 
} 

$output應該是格式化您的規格陣列。

+0

我不在哪裏我犯了錯誤(我'一個新的PHP學習者)。注意結果顯示。 –

+0

但是謝謝你的幫助@FatAdama –

+0

也許如果你試圖回顯輸出而不是將它分配給一個新的數組?首先迭代數組可能會非常棘手。 –