今天我有一個很奇怪的問題。我有一個序列化數組,看起來像這樣:非串行陣列給出了最奇怪的結果
a:4:{i:0;s:7:"Perfect";i:1;s:10:"jawel hoor";i:2;s:14:"Ach jawohl joh";i:3;s:2:"Ja";}
再經過我執行此代碼:
include '../../database/connect.php';
Class Calc {
function getPrice($id) {
$Db = new Db();
$sth = $Db->dbh->prepare("SELECT * FROM orders WHERE id = :id");
$sth->execute(array(':id'=>$id));
$are_you_serial = $sth->fetchAll();
foreach($are_you_serial as $row) {
$serialised = $row['reply_array'];
$product_id = $row['product_id'];
$user_id = $row['user_id'];
}
$array = unserialize($serialised);
foreach($array as $row) {
echo $row[1];
}
}
}
$calc = new Calc();
$calc->getPrice(11);
EACA出來。當我打電話0行PjAJ出來。 當我打電話第2行,這似乎是錯誤:
rwh
Notice: Uninitialized string offset: 2 in index.php on line 29
這是陣列的樣子,如果我只是print_r的在$數組:
Array
(
[0] => Perfect
[1] => jawel hoor
[2] => Ach jawohl joh
[3] => Ja
)
我也可以調用$數組[0 ],它會顯示正確的東西,但一旦我把它放在foreach中,它就不再起作用了。
'foreach($ array as $ row){ echo $ row [1]; ($ array as $ row){ echo $ row; }' –
你正在查詢什麼可能(可能)只有一行數據。你爲什麼使用fetchAll()和一個循環來記錄單個記錄?爲什麼不只是'$ row = $ sth-> fetch()'? –
因爲我也需要user_id和product_id @MarcB //編輯:nvm我也很笨,謝謝你:P – user2879055