-1

I have a php code as shown below in which I am getting the error Undefined variable: current abc.php at LineA.

if (isset($helloWorld[$this->index])) {
    $current = $helloWorld[$this->index];
}

if (isset($current)) {
    if (!is_array($current)) {
        return $list;
    }
}
foreach ($current as $key => $values) {                     // LineA
    $attName = $helloChamp[$key];

    foreach ($values as $value) {
        $valName = $helloHow[$key][$value];
        $list[$attName][] = $valName;
    }
}

This is what I have tried so that I can fix the problem of undefined variable at LineA above. I have added foreach loop inside the if block at LineZ. Let me know if its the right away to fix the Undefined variable problem for this particular case.

if (isset($helloWorld[$this->index])) {
    $current = $helloWorld[$this->index];
}

if (isset($current)) {
    if (!is_array($current)) {
        return $list;
    }
}
if (isset($current)) {                                           // Line Z
    foreach ($current as $key => $values) {                     // LineA
        $attName = $helloChamp[$key];

        foreach ($values as $value) {
            $valName = $helloHow[$key][$value];
            $list[$attName][] = $valName;
        }
    }
}
flash
  • 1,455
  • 11
  • 61
  • 132

1 Answers1

0

Have you tried initializing $current before the if statement ?

Like this:

$current = []; // Initialize $current here before you use it anywere...
if (isset($helloWorld[$this->index])) {
    $current = $helloWorld[$this->index];
}

Then you can remove the if statements, since the $current variable would always be an array. So, It would be like this:

$current = [];
$list = [];

if (isset($helloWorld[$this->index])) {
    $current = $helloWorld[$this->index];
}

foreach ($current as $key => $values) {
    $attName = $helloChamp[$key];

    foreach ($values as $value) {
        $valName = $helloHow[$key][$value];
           $list[$attName][] = $valName;
    }
}
Akash R
  • 46
  • 1
  • 7