-2

I understand that the each function() is deprecated in PHP 8.0.

Updated: I'm getting a Fatal error: Uncaught Error: Call to undefined function each() line 742 - [line 742 is this line in the code snippet below: list($orig,$values) = each($where); ]

I am trying to replace it in the below code with foreach() as suggested in this post, but I am not experienced enough in PHP or coding to achieve this. Can anyone help?

public function getMetaboxConfig($type) {
    static $cache;

    if (!empty($cache[$type])) {
        return $cache[$type];
    }

    do_action("pe_theme_metabox_config_$type");

    $config =& PeGlobal::$config;
    $metaboxes = PeGlobal::$config["metaboxes"];

    $pmboxes = empty($config["metaboxes-$type"]) ? null : $config["metaboxes-$type"];

    if ($custom = apply_filters("pe_theme_metabox_$type",$pmboxes)) {
        //print_r(array_keys(PeGlobal::$config["metaboxes-view"]));
        $keys = array_keys($custom);
        foreach ( $custom as $key => $value ) {
            $metaboxes[$key] = $custom[$key];
            $where =& $metaboxes[$key]["where"];
            list($orig,$values) = each($where);
            if ($orig != $type) {
                unset($where[$orig]);
                $where[$type] = $values;
            }
        }
    }
    $cache[$type] = $metaboxes;
    return $metaboxes;

}

The error I am seeing:

Error message

Ed_B
  • 1
  • 1
  • If you are having problems, what are the results you get and what are you expecting. Also include any errors which may help. – Nigel Ren Jan 18 '23 at 15:50
  • 1
    It would also help us if you added the original code with `each` to the question, like this we could compare andd see what is wrong – Kaddath Jan 18 '23 at 15:52
  • Apologies, I have edited my original post with the entire snippet. – Ed_B Jan 18 '23 at 16:00
  • 1
    A design note: Instead of `$keys = array_keys($custom); foreach ($keys as $key) {..}` you can use `foreach ( $custom as $key => $value ) {...}`. In this case `$value` replaces `$custom[$key]`. – Wiimm Jan 18 '23 at 16:28
  • Thanks Wim, I have updated the code accordingly, but I'm still getting an error on line 742 Fatal error: Uncaught Error: Call to undefined function each() https://www.shelbournegardens.com/ [line 742 is this in snippet above: list($orig,$values) = each($where); ] – Ed_B Jan 18 '23 at 17:21
  • FWIW [Rector](https://github.com/rectorphp/rector) is a super-handy tool that can do stuff like this for you. – Alex Howansky Jan 18 '23 at 17:35
  • I've updated my original post with the error I am receiving. – Ed_B Jan 18 '23 at 20:34

1 Answers1

0

This is an exemple to foreach :

<?php


$myArr = [
  'key1' => 'value1',
  'key2' => 'value2',
];

foreach($myArr as $k => $v){
  //first iteration : $k = 'key1', $v = 'value1'
  //second iteration : $k = 'key2', $v = 'value2'
  ... your code
}

So, you don't need to use array_keys :

foreach ($custom as $key => $value) {
    $metaboxes[$key] = $value;
    ...
}
svgta
  • 343
  • 1
  • 6