2013-09-29 121 views
2

有些時候,編程中的小事情會得到巨人。我在2維數組上玩,但我無法得到我需要的東西。循環通過php中的多維數組

下面是我的數組結構。

Array 
     (
    [0] => Array 
    (
     [0] => 16 
     [id] => 16 
     [1] => 1 
     [userid] => 1 
     [2] => [email protected] 
     [email] => [email protected] 
     [3] => dffsdf 
     [message] => dffsdf 
     [4] => 0 
     [status] => 0 
    ) 

[1] => Array 
    (
     [0] => 17 
     [id] => 17 
     [1] => 1 
     [userid] => 1 
     [2] => [email protected] 
     [email] => [email protected] 
     [3] => dffsdfnnnnnnnnnnn 
     [message] => dffsdfnnnnnnnnnnn 
     [4] => 0 
     [status] => 0 
    ) 
) 

我在這裏做的是獲取具有某個id的用戶的消息。我這樣做像

if($get_mails[0]['userid'] == $_GET['userid']) 
    { 

$last_key = end(array_keys($get_mails)); 

echo '{"Messages":['; 

foreach($get_mails as $key => $get_each_mail){ 

$company_name = $get_each_mail['company_name']; 
$email_id = $get_each_mail['id']; 
$email_body = $get_each_mail['message']; 
} 
echo '{"CompanyName":"'.$company_name.'","MessageID":"'.$email_id.'","MessageBody":"'.$email_body.'"'; 

if ($key == $last_key) 
{ 
    echo '}]}'; 
}else{ 
    echo'},'; 
    } 
} 

什麼,我不能這樣做太有趣了,我需要一個環[0]在這行代碼

if($get_mails[0]['userid'] == $_GET['userid']) 

if($get_mails[i]['userid'] == $_GET['userid']) and it give me all the records against specific user. 

這裏就是我想爲特定用戶

{"Messages":[{"CompanyName":"newtech","MessageID":"14","MessageBody":"hi how are you"},{"CompanyName":"newtech","MessageID":"15","MessageBody":"hi how are you"},{"CompanyName":"newtech","MessageID":"24","MessageBody":"asfasdfsdfsdfsdfsdfsdfsdfsd"}]} 

就像這樣,如果有更多的記錄可用於特定的用戶,它會增加越來越多。

+3

你想讓JSON? – Sumurai8

+0

是的,我正在製作jason –

+2

使用'json_encode()'而不是試圖回顯正確的字符。你可以在問題中添加所需的json結構嗎? – Sumurai8

回答

1

假設$get_mails包含您發佈以上(含company_name)陣列,你可以寫這樣的事情:

$output = Array("Messages" => Array()); 
foreach($get_mails as $k => $arr) { 
    $t = Array(); 
    $t['CompanyName'] = $arr['company_name']; 
    $t['MessageID'] = $arr['id']; 
    $t['MessageBody'] = $arr['message']; 

    $output['Messages'][] = $t; 
} 

echo json_encode($output); 

首先,你與你的JSON的結構準備數組。語法$array[] = a將追加a$arrayjson_encode(...)最後會照顧到將其轉換爲有效的JSON,即使其中一個鍵包含引號或JSON中無效的其他特殊字符也是如此。

我相信你只想顯示來自某個用戶的消息,並試圖用if($get_mails[0]['userid'] == $_GET['userid'])來實現。我建議改變你的SQL查詢的東西,實現了這個,因爲如果你試圖通過用下面的代碼的所有郵件檢索您的網頁的性能會大大增加:

$output = Array("Messages" => Array()); 
foreach($get_mails as $k => $arr) { 
    if($arr['user_id'] == $_GET['userid']) { 
    $t = Array(); 
    $t['CompanyName'] = $arr['company_name']; 
    $t['MessageID'] = $arr['id']; 
    $t['MessageBody'] = $arr['message']; 

    $output['Messages'][] = $t; 
    } 
} 

echo json_encode($output); 
+0

我解決了我的問題,除了}之外,我的代碼中沒有錯誤。順便感謝每一位同仁的好心和準時的建議。 Thaks喋喋不休,還有你們所有人。 –

+0

這是我正確的代碼,如果($ get_mails) { \t \t $ last_key =結束(array_keys($ get_mails)); \t echo'{「Messages」:['; \t foreach($ get_mails as $ key => $ get_each_mail){ \t $ company_name = $ get_each_mail ['company_name']; \t $ email_id = $ get_each_mail ['id']; \t $ email_body = $ get_each_mail ['message']; \t \t echo'{「CompanyName」:「'。$ company_name。'」,「MessageID」:「'。$ email_id。'」,「MessageBody」:「'。$ email_body。'」'; \t \t 如果($鍵== $ last_key) \t { \t回波 '}]}'; \t} else { \t \t echo'},'; \t \t} \t} \t \t \t \t } –