0
可以說我有這樣的代碼:PDOStatement對象::使用fetchall()之後施加一個PHP函數到一列
<?php
$sth = $dbh->prepare("SELECT name, colour FROM fruit");
$sth->execute();
/* Fetch all of the remaining rows in the result set */
print("Fetch all of the remaining rows in the result set:\n");
$result = $sth->fetchAll();
print_r($result);
?>
其輸出這樣的:
Array
(
[0] => Array
(
[name] => pear
[0] => pear
[colour] => green
[1] => green
)
[1] => Array
(
[name] => watermelon
[0] => watermelon
[colour] => pink
[1] => pink
)
)
此代碼之前,我已經設計了下面這個簡單的功能:
function changecolour($data){
if($data == 'pink'){ $data = 'red'; }
return $data
}
問題: 如何申請功能的陣列只有密鑰[colour]
(適用於數據庫中的一列)並使用新列修復整個數組?另外,如果數組很大,那麼你的方法可能會產生什麼影響?如果你正在使用樹枝,你會如何建議在mysql表的一列上應用一個函數?
嘗試使用array_filter PHP函數。 – Indrajit