php将获取对象键转换为数字

时间:2017-09-18 05:11:04

标签: php arrays

我有以下代码从db获取记录:

$items = $post->get_list('news', $lang);

while($item = $items->fetch_object()){
    $res[] = $item;
}

foreach($res as $re){
    echo $re->pid.'<br>';
    echo $re->title.'<br>';
    echo $re->content.'<br>';
}

基于某些情况,我需要以$re[2]形式而不是$re->content获取数据,根据我的需要转换数据的正确方法是什么?

2 个答案:

答案 0 :(得分:0)

你应该使用mysqli_fetch_array()代替fetch_object(),因为fetch_object()以对象的形式返回结果。所以要以数组的形式获得结果,你应该使用mysqli_fetch_array()或mysqli_fetch_assoc()。

答案 1 :(得分:0)

您正在使用返回对象格式的fetch_object。如果您想要数组格式,可以使用fetch_row

while($item = $items->fetch_row()){
    $res[] = $item;
}

see here