array_splice behavior in PHP 4.x

30 Views Asked by At

I'm in the process of migrating an old piece of code to PHP 8.1 from (hold on to your hat...) PHP 4.2!

However I did make significate progress. I ran in to the following statement

if ($arr[$key]) {
    array_splice($arr[$key]);
}

var_dump shows

$key = 'a';
$arr = ['a' => 1];

Obviously this breaks on 8.1 for 2 reasons

  1. int is passed as an array.
  2. offset is ignored.

I'm having trouble under standing what would have been the behavior of this code under PHP 4.2
Looking at the 4.x documentation of array_splice does not explain.

Thank's

1

There are 1 best solutions below

0
phper On

It emits a warning so it would simply skip this line and continue execution

MIgrated code looks like this

if ($arr[$key] && is_array($arr[$key])) {
    array_splice($arr[$key], 0);
}