【问题标题】:Notice: Undefined offset in array php注意:数组 php 中未定义的偏移量
【发布时间】:2015-04-27 01:05:17
【问题描述】:

我收到此错误:

注意:数组 php 中未定义的偏移量

有人知道我的错误在哪里吗?

//$forme count of my array 
for( $counter = 0; $counter < $forme; $counter++ )  {

    if($az[$counter] > 0)
    {
        $test=$connect->fetch_all("select * from `tbl_jobs_numbers` where `sub_id`='".$Subid[$counter]."' && `job_id`='".$JobID."' && `city_id`='".$ostan."' limit '".$az [ $counter ]."' , '".$ta[ $counter ]."'") ;

        foreach($test as $puriya)
        {
            print $mobiles=$puriya['number']."<br/>";
        }
    }
    else
    {
        $test=$connect->fetch_all("select * from `tbl_jobs_numbers` where `sub_id`='".$Subid[$counter]."' && `job_id`='".$JobID."' && `city_id`='".$ostan."' ") ;

        foreach($test as $puriya)
        {
            //print $mobiles=$puriya['number']."<br/>";
        }
    }
}

【问题讨论】:

  • 我这里有错误 if($az[$counter] > 0) {
  • $az 定义在哪里?
  • @$az=$security->Check_Post($_POST['az']); @$ta=$security->Check_Post($_POST['ta']);
  • 您显示的错误表明您的错误在您没有向我们展示的 sn-p 中,因为它表明错误在 for 循环中,我似乎不能在示例中找到相关代码。另外,请通过更正式和清晰的方式来整理问题。
  • Check_Post 返回什么?我们需要更多你的代码,或者它返回一个字符串、int、非数组,这就是问题所在。

标签: php arrays undefined offset


【解决方案1】:

如果您想遍历数组的每个项目,更简单的方法是使用 foreach(基于 not :

// array_values to make sure that the keys are numeric
foreach (array_values($az) as $index => $item) {
    if ($item > 0) {
        $test=$connect->fetch_all("select * from `tbl_jobs_numbers` where `sub_id`='".$Subid[$index]."' && `job_id`='".$JobID."' && `city_id`='".$ostan."' limit '".$az[$index]."' , '".$ta[$index]."'") ;

        foreach($test as $puriya)
        {
            print $mobiles=$puriya['number']."<br/>";
        }
    } else {
        $test=$connect->fetch_all("select * from `tbl_jobs_numbers` where `sub_id`='".$Subid[$index]."' && `job_id`='".$JobID."' && `city_id`='".$ostan."' ") ;

        foreach($test as $puriya)
        {
            //print $mobiles=$puriya['number']."<br/>";
        }
    }
}

这应该避免$az 的未定义索引错误。

【讨论】:

    猜你喜欢
    • 2023-03-27
    • 1970-01-01
    • 2023-03-13
    • 1970-01-01
    • 1970-01-01
    • 2016-10-01
    • 1970-01-01
    • 2015-08-01
    相关资源
    最近更新 更多