我有一个代码示例: 银行(身份证,姓名)
function getListBank() {
$data = array();
$sql = 'Select id, name From bank';
$query = mysql_query($sql);
if(!$query) {
echo "Error: " . mysql_error();
exit;
}
while($row = mysql_fetch_object($query)) {
$data[] = $row;
}
return $data;
}
$banks = getListBank();
$convert = array();
foreach ($banks as $bank) {
$convert[] = $bank->name;
}
foreach ($convert as $k => $v) {
echo "key:" . $k . "value:" . $v;
}
我无法从数组foreach中获取值和键。有什么想法吗?
答案 0 :(得分:0)
我认为这应该是:
$convert[] = $bank['name'];
我从未在foreach循环中使用属性语法。
答案 1 :(得分:0)
使用:
foreach ($banks as $bank) {
$convert[$bank->id] = $bank->name;
}