1

Getting the following notice in PHP 7.4:

Notice: Only variables should be passed by reference

In:

$roots = array();
foreach ($data as $row) {   
    $row->type = end(explode(',',(implode(array_slice(explode ( ':',  $row->global_id), -2, 1)))));
    if ($row->parent_global_id === null) {
        $roots[]= $row;
    } else {
        $data[$row->parent_global_id]->children[] = $row;
    }
    unset($row->parent_global_id);
    unset($row->global_id);
}

at line:

$row->type = end(explode(',',(implode(array_slice(explode ( ':',  $row->global_id), -2, 1)))));

I have seen other questions on this at SO, but can't quite figure out how to fix it in this instance. Ideas?

IlludiumPu36
  • 4,196
  • 10
  • 61
  • 100
  • Try creating a [mcve]. – outis Jul 15 '22 at 01:52
  • that's it in this instance – IlludiumPu36 Jul 15 '22 at 01:53
  • what is the structure of `$row`? – Raptor Jul 15 '22 at 01:54
  • `end` is a little weirder than other functions because it not only returns the last, it also resets the array pointer and thus requires a reference variable to act upon. The short answer is that you have to break this into a two lines, a variable assignment followed by the `end` call. – Chris Haas Jul 15 '22 at 01:55
  • The sample isn't minimal (as explained in the linked article). A minimal example would consist of something like a call to `end` taking the result of an `explode`. – outis Jul 15 '22 at 01:59
  • Also covered by "[Only variables should be passed by reference](https://stackoverflow.com/q/4636166/90527)". – outis Jul 15 '22 at 02:02

1 Answers1

3

Assign the explode output to a variable. Send the variable to the end function.

$roots = array();
foreach ($data as $row) {
    $var = explode(',',(implode(array_slice(explode ( ':',  $row->global_id), -2, 1))));
    $row->type = end($var);
    if ($row->parent_global_id === null) {
        $roots[]= $row;
    } else {
        $data[$row->parent_global_id]->children[] = $row;
    }
    unset($row->parent_global_id);
    unset($row->global_id);
}
sercan
  • 182
  • 19