Home > database >  How to exchange deprecated each function
How to exchange deprecated each function

Time:10-01

I'm trying to fix the code which use deprecated each. It would be great if someone can help here. I looked on similar topics but any of solution didn't work. And I couldn't find exact example of the line I have problem with.

Here is the part of the code which stoped working because each is deprecated in newer php versions:


# ----------- Draw the bars here ------
        for($i = 0; $i < $total_bars; $i  ){

            list($key, $value) = each($values);

            $x1 = $margins   $gap   $i * ($gap   $bar_width) ;
            $x2 = $x1   $bar_width;
            
            $y1 = $margins   $graph_height- intval($value * $ratio);
            $y2 = $img_height - $margins;
            
            $vallen = strlen($value);
            $keylen = strlen($key);
            
            $bar_half = $bar_width / 2;
            
            $valoffset = ($vallen >= $bar_width ? 0 : ($bar_width - $vallen)/2);
            $keyoffset = ($keylen >= $bar_width ? 0 : ($bar_width - $keylen)/2);
                
            imagettftext($img, 10, 0, $x1   $valoffset, $y1 - 10, $bar_color, $font_file, $value);
            imagettftext($img, 10, 0, $x1   $keyoffset - 20, $img_height - 15, $bar_color, $font_file, $key);
            
            imagefilledrectangle($img, $x1, $y1, $x2, $y2, $bar_color);
        }
            
        imagejpeg($img, $filename, 100);
    }
}

Im having problems with this line:

   list($key, $value) = each($values);

Here is complete code:


public function createBarChart($data, $filename, $title)
    {
        # ------- The graph values in the form of associative array
        $values = $data;
        $total_bars = count($values);
        
        
        $img_width = 820;
        $img_height = 300;
        
        $bar_width = ($img_width/(2*$total_bars)); //30;
        $margins = 30;
        
        $font_title = 3;
        $font_text = 1;
        
        # ---- Find the size of graph by substracting the size of borders
        $graph_width = $img_width - $margins * 2;
        $graph_height = $img_height - $margins * 2;
        $img = imagecreate($img_width, $img_height);

        
        $gap = ($graph_width - $total_bars * $bar_width ) / ($total_bars  1);
        
        
        # -------  Define Colors ----------------
        $bar_color = imagecolorallocate($img, 0, 0, 0);
        $background_color = imagecolorallocate($img, 255, 255, 255);
        $border_color = imagecolorallocate($img, 255, 255, 255);
        $line_color = imagecolorallocate($img, 220, 220, 220);
        $grid_color = imagecolorallocate($img, 255, 255, 255);
        $title_color = imagecolorallocate($img, 255, 77, 77);
        
        # ------ Create the border around the graph ------
        
        imagefilledrectangle($img, 0, 0, $img_width, $img_height, $background_color);
        imagefilledrectangle($img, $margins, $margins, $img_width-1-$margins, $img_height-1-$margins,$background_color);
        
        imageline($img, 1, 1, $img_width, 1, $bar_color);
        imageline($img, 1, $img_height - 1, $img_width, $img_height - 1, $bar_color);
        
        # ------- Max value is required to adjust the scale -------
        $max_value = max($values);
        $ratio = $graph_height/$max_value;
        
        
        # -------- Create scale and draw horizontal lines  --------
        $horizontal_lines = 20;
        $horizontal_gap = $graph_height/$horizontal_lines;
        
        $y = $img_height - $margins;
        imageline($img, $margins, $y, $img_width - $margins, $y, $grid_color);
    
        $font_file = public_path() . '/../template/fonts/Raleway-Light.ttf';
        
        # ----------- Draw the bars here ------
        for($i = 0; $i < $total_bars; $i  ){
        
            list($key, $value) = each($values);

            $x1 = $margins   $gap   $i * ($gap   $bar_width) ;
            $x2 = $x1   $bar_width;
            
            $y1 = $margins   $graph_height- intval($value * $ratio);
            $y2 = $img_height - $margins;
            
            $vallen = strlen($value);
            $keylen = strlen($key);
            
            $bar_half = $bar_width / 2;
            
            $valoffset = ($vallen >= $bar_width ? 0 : ($bar_width - $vallen)/2);
            $keyoffset = ($keylen >= $bar_width ? 0 : ($bar_width - $keylen)/2);
                
            imagettftext($img, 10, 0, $x1   $valoffset, $y1 - 10, $bar_color, $font_file, $value);
            imagettftext($img, 10, 0, $x1   $keyoffset - 20, $img_height - 15, $bar_color, $font_file, $key);
            
            imagefilledrectangle($img, $x1, $y1, $x2, $y2, $bar_color);
        }
            
        imagejpeg($img, $filename, 100);
    }
}

CodePudding user response:

Since $total_bars is just count($values) you can simply replace:

for($i = 0; $i < $total_bars; $i  ){
    list($key, $value) = each($values);
    // ...
}

With:

$i = 0;
foreach( $values as $key => $value ) {
    // ...
      $i;
}

If you wanted to keep the for loop you could also:

$keys = array_keys($values);
for($i = 0; $i < $total_bars; $i  ){
    $key   = $keys[$i];
    $value = $values[$key];
    // ...
}
  • Related