Foreach中未定义数组//为foreach()提供的无效参数

时间:2017-05-25 14:39:49

标签: php

  

注意:未定义的变量:parts3

     

警告:为foreach()提供的参数无效

前两个阵列没有错; $parts$parts2。但是当谈到$parts3时,它对于foreach来说是不确定的。

$round[0]=5;
$round[1]=4;
$round[2]=6;
$round[3]=2;

$slice=3;
$count=0;
$partscount=0;
$parts=explode( ',', $round ) ;
$store; 
$parts2;
$parts3; 

foreach($parts as $index => $value):        

    if($value>$slice) {
        $store[$count]=$slice;              
        $parts2[$partscount]=$value-$slice;
        $partscount++;
        $count++;
    } else {
        switch ($value){
            case 1:
                $store[$count]=1;
                $count++;
                break;
            case 2:
                $store[$count]=2;
                $count++;
                break;
        }
    }    

endforeach;


foreach($parts as $index => $value):        

    if($value>$slice){
        $store[$count]=$slice;          
        $count++;
        $partscount++;
        $parts3[$partscount]=$value-$slice;
    } else {
        switch ($value){
            case 1:
                $store[$count]=1;
                $count++;
                break;
            case 2:
                $store[$count]=2;
                $count++;
                break;  
        }
    }

endforeach;

foreach($parts3 as $index => $value):       

    $cut=$value;
    if($value>$slice){
        $store[$count]=$slice;          
        $count++;   
        $partscount++;
        $parts4[$partscount]=$value-$slice;
    } else {
        switch ($value){
            case 1:
                $store[$count]=1;
                $count++;
                break;
            case 2:
                $store[$count]=2;
                $count++;
                break;
        }
    }

endforeach; 

2 个答案:

答案 0 :(得分:0)

explode()以[{1}}为参数,string$round,您可能需要使用:

array

注意:我不会检查您的所有代码,因为我不了解您正在做的事情,但错误已经消失且$round[]=5; $round[]=4; $round[]=6; $round[]=2; $slice=3; $count=0; $partscount=0; $store; $parts2; $parts3; foreach($round as $index => $value) { if ($value > $slice) { $store[$count] = $slice; $parts2[$partscount] = $value - $slice; $partscount++; $count++; } else { switch ($value) { case 1: $store[$count] = 1; $count++; break; case 2: $store[$count] = 2; $count++; break; } } } 输出:

print_r($store)

答案 1 :(得分:0)

parts3永不填充,

中有错误

foreach( $ parts2 as $ values ):

if(**$value>$slice**){