2017-03-04 75 views
0

我想解碼一個在javascript中發送併發送到php的對象數組,但是我無法正確解碼它。 與對象的數組看起來像這樣:enter image description here在php中解碼ajax後的問題

,我使用來獲取信息的PHP代碼是這樣的:

$allInformation = $_POST['information']; 

//this works 
$user = $allInformation[count($allInformation)-2]; 
$num = $allInformation = [count($allInformation)-1]; 
//this doesnt work 
$array = var_dump(json_decode($allInformation)); 

陣列1和2工作的指標就好了,但是,我似乎不能解碼作爲對象的索引0。我該怎麼辦?

+0

你怎麼發送它?可能你需要'JSON.stringify'信息,並做 'json_decode($ _ POST ['information'])'; –

+0

給予更多的信息,比如預期結果是什麼,你得到了什麼錯誤? – C2486

+0

我使用以下格式的ajax發送它:'var data = [];'在我的循環中收集我的數據,我使用數據數組插入以下內容:'data [i] = {'user':username, '日期':日期}'。然後我將數據按原樣傳遞給ajax進行發佈。 – Aboogie

回答

0

得到它的工作,被JSON字符串化的php數組是[{「user」:「user」,「date」:「03/17/2017」},「user」,134997] $ update = json_decode($ _ POST ['information']);然後循環它以獲取用戶和日期作爲對象,即$ update [$ i] - > date。謝謝所有