简体   繁体   中英

Array is Undefined in Foreach // Invalid argument supplied for foreach()

Notice: Undefined variable: parts3

Warning: Invalid argument supplied for foreach()

There is nothing wrong with the first two array; $parts and $parts2 . But when it comes to $parts3 it is undefined to the 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; 

explode() takes a string as argument and $round is an array , you may want to use:

$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;
        }
    }
}

Note: I don't check all your code because I don't understand what you're doing, but the error is gone and print_r($store) outputs:

Array
(
    [0] => 3
    [1] => 3
    [2] => 3
    [3] => 2
) 

parts3 never fill, you have an error in

foreach( $parts2 as $values ):

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM