2014-03-06 61 views
-1

當使用json_encode獲取只有字符串的鍵而不是重複的值,但使用數字鍵的條目時,有什麼辦法嗎?MySQLi json_encode - 如何獲得唯一的字符串鍵不是數字鍵?

這裏的SQL和PHP:

$stmt = $db->prepare("SELECT employee_profile.first_name,employee_profile.last_name, DATE(employee_timecard_entry.time_in_timestamp) AS date, employee_timecard_entry.hours_worked 
     FROM employee_timecard_entry, employee_profile WHERE time_in_timestamp BETWEEN ? AND ? AND employee_timecard_entry.employee_id = employee_profile.employee_id"); 
    $stmt->bind_param("ss", $left_side, $right_side); 
    $stmt->execute(); 
    $result = $stmt->get_result(); 
    $data = array(); 

    while($row = mysqli_fetch_array($result)) 
     $data[] = $row; 
    header('Content-type: application/json'); 
    echo json_encode($data); 

和輸出的一個樣本:

[ 
    { 
     "0": "Ashley", 
     "1": "Boehme", 
     "2": "2014-01-04", 
     "3": "6.10", 
     "first_name": "Ashley", 
     "last_name": "Boehme", 
     "date": "2014-01-04", 
     "hours_worked": "6.10" 
    },... 

我寧願只有最後4項,而不是所有8.我已經搜索並找到了一種方法來與PDO做到這一點,但是我對這個項目太過分了,無法改變。我還不想在客戶端對它們進行排序,還是唯一的方法?

回答

0

你需要的是mysqli_fetch_assoc &不mysqli_fetch_array

while($row = mysqli_fetch_assoc($result)) 
+0

哇...那麼容易吧。謝謝 – tannerwj

相關問題