关于增加数组元素指定key

$data = array();
$data[$a] = $b;
print_r($data);
结果
Array(
    $a => $b
)

测试OK:

// 开始测试读取数据
include_once('../conf/connect.php');
// $res = $pdo->query("select id,pv from `chart_pie`",PDO::FETCH_ASSOC);
$qry = "select id,pv from `chart_pie`";
$res = $pdo->query($qry)->fetchAll(PDO::FETCH_ASSOC);
// $reses = $res->(PDO::FETCH_ASSOC);
var_dump($res);
$resArr = array();
foreach ($res as $key => $value) {
    // array_push($resArr,[$value['id'],$value['pv']]);
    $resArr[$value['id']] = $value['pv'];//指定键名
    
}

你可能感兴趣的:(关于增加数组元素指定key)