关联数组第一个元素不是从 0 开始

我的关联数组是使用函数填充的。它的第一个元素键是空白的,我需要它是0.


$blocks= get_data_ordered('blocks', 'page_id', $page, '', 'sequence');


highlight_string("<?php\n\$blocks =\n" . var_export($blocks, true) . ";\n?>");



function get_data_ordered($table, $condition_field, $condition_value, $limit, $order) {

include 'conf/config.php';

include 'conf/opendb.php';


if (!$condition_field){

    $condition = "";

}

else{

    $condition = "WHERE `$condition_field`='$condition_value'";

}


if($limit){

    $limit_query="LIMIT $limit";

    $i=0;

}


  $result=mysqli_query($conn, "SELECT * FROM $table $condition ORDER BY $order ASC $limit_query");

while($row = mysqli_fetch_array($result, MYSQLI_ASSOC))

{

    if($limit==1){

        return $row;

    }

    else{

        $output[$i] = $row;

        $i++;

    }

}

return $output;

include 'conf/closedb.php';

}

我目前的输出如下。第一个元素键是空白而不是 0


$blocks =

array (

  '' => 

  array (

    'id' => '2',

  ),

  1 => 

  array (

    'id' => '6',

  )

);

我需要的是从 0 开始而不是空白键


$blocks =

array (

  0 => 

  array (

    'id' => '2',

  ),

  1 => 

  array (

    'id' => '6',

  )

);

请帮我解决这个问题


慕工程0101907
浏览 157回答 3
3回答

眼眸繁星

发现错误:内部函数$i在第一个实例中为空

MMMHUHU

您有 2 个选择:首先就像上面评论的@AliveToDie,您不需要在$i这种情况下使用&nbsp;。您可以使用&nbsp;$output[] = $row;其次是如果您$i出于自己的原因需要使用,则需要定义$i = 0外部&nbsp;if($limit) {..}条件
打开App,查看更多内容
随时随地看视频慕课网APP