我有看起來像這樣的一個鍵值表的數組(從DB直來):減少陣列深入
$application_strings = array(
array('key' => 'building_type', 'value' => 'Building Type'),
array('key' => 'application', 'value' => 'Application'),
);
/* would print_r like so:
Array(
[0] => Array([key] => 'building_type', [value] => 'Building Type'),
[1] => Array([key] => 'application', [value] => 'Application')
)
*/
,並想將它轉化成類似
$strings = array(
'building_type' => 'Building type',
'application' => 'Application',);
/* would print_r like so:
Array(
[building_type] => 'Building Type',
[application] => 'Application'
)
*/
我目前的做法是這樣的:
$strings_statement = DB::getDB()->prepare(
"SELECT `key`, `value` FROM `strings`
WHERE `lang` = (SELECT `lang` FROM `sites` WHERE `key`=:key)");
$strings_statement->execute(array(':key' => 12345));
$application_strings = $strings_statement->fetchAll(PDO::FETCH_ASSOC);
$strings = array();
foreach($application_strings as $string) {
$strings[$string['key']] = $string['value'];
}
可以這樣做短/更好?
一個更好的解決方案是'SELECT'輸出你想要的而不是重新安排PHP中的數據集。你能顯示查詢嗎? – Martin 2012-07-19 14:16:58
如果你用你的方法做'print_r($ strings,true)' - 你會得到什麼? – ethrbunny 2012-07-19 14:23:34
添加了上面的查詢。如果我可以相應地修改查詢,那就太好了。但是,由於我不太擅長SQL,因此我首先在PHP端進行了修改。 – fragmentedreality 2012-07-19 14:24:17