Skip to content

PHP 8.5 array_column Deprecation warning: Using null as an array offset is deprecated, use an empty string instead #25144

Closed as not planned

Description

@clarumedia

Description

The following code:

<?php
ini_set('display_errors', '1');
ini_set('display_startup_errors', '1');
error_reporting(E_ALL);

$arr=[
	11=>[
		"a"=>"Blaa",
		"b"=>"Inco",
	],
	12=>[
		"a"=>"XXX",
		"b"=>null,
	]
];
print_r(array_column($arr ?? [],null,"b"));

Resulted in this output:

Deprecated: Using null as an array offset is deprecated, use an empty string instead in /x/y/z/test.php on line 16
Array ( [Inco] => Array ( [a] => Blaa [b] => Inco ) [] => Array ( [a] => XXX [b] => ) ) 

But I expected this output instead:

Array ( [Inco] => Array ( [a] => Blaa [b] => Inco ) [] => Array ( [a] => XXX [b] => ) ) 

If the intention is to blow up at this point in future PHP versions then it's going to add a lot of maintenance dev and extra overhead to fix it.  To what advantage ? 
If I'm going to have to loop through the array checking for nulls each time then I may as well dispense with array_column completely and write my own function - which will be nowhere near as performant as the existing PHP function.

PHP Version

PHP 8.5.12 (cli) (built: Oct 21 2026 14:36:25) (NTS)
Copyright (c) The PHP Group
Built by Ubuntu
Zend Engine v4.5.12, Copyright (c) Zend Technologies
    with Zend OPcache v8.5.12, Copyright (c), by Zend Technologies

Operating System

No response

Activity

  1. arshidkv12 commented on Nov 2, 2026

    @arshidkv12
    Contributor

    This deprecation was intentionally introduced in 49e3956 as part of the PHP 8.5 deprecations. See the changes.

  2. clarumedia commented on Nov 2, 2026

    @clarumedia
    Author

    2. This deprecation was intentionally introduced in 49e3956 as part of the PHP 8.5 deprecations. See the changes.

    I'm aware of that.
    The point I am making is: If the intention is to blow up at this point in future PHP versions, it means I'm going to have to loop through the array checking for nulls each time so I may as well dispense with array_column completely and write my own function - which will be nowhere near as performant as the existing PHP function.
    What's your solution ?
    Thank you

  3. arshidkv12 commented on Nov 2, 2026

    @arshidkv12
    Contributor

    The deprecation is related to using null as an array key, not to array_column() itself. Since array_column() uses the values from the specified index column as keys in the resulting array, a null value in that column triggers the deprecation.

    To avoid it, you would need to normalize the keys before using array_column(). I understand your concern about the additional iteration and performance overhead.

  4. clarumedia commented on Nov 2, 2026

    @clarumedia
    Author

    4. The deprecation is related to using null as an array key, not to array_column() itself. Since array_column() uses the values from the specified index column as keys in the resulting array, a null value in that column triggers the deprecation.
    To avoid it, you would need to normalize the keys before using array_column(). I understand your concern about the additional iteration and performance overhead.

    Thanks for your commentary. What's the point of using array_column if I'm going to have to iterate the array anyway to prevent it blowing up on nulls in future PHP versions ? I may as well just re-write array_column so it carries on working as it has for the past 13 years ? Is there any other solution ?

  5. Girgias commented on Nov 2, 2026

    @Girgias
    Member

    array_column already blows up if you have invalid types: https://3v4l.org/GZi8D#v

    Ideally you array shouldn't even be containing such values if you are going to use array_column, one of the many static analysis tools out there can help you identify the root cause.

    As such I'm marking this as Won't fix.

  6. clarumedia commented on Nov 2, 2026

    @clarumedia
    Author

    6. array_column already blows up if you have invalid types: https://3v4l.org/GZi8D#v
    Ideally you array shouldn't even be containing such values if you are going to use array_column, one of the many static analysis tools out there can help you identify the root cause.
    As such I'm marking this as Won't fix.

    I know exactly why there are nulls in the data. It's not going away. Goodbye array_column :-(

  7. MorganLOCode commented on Nov 2, 2026

    @MorganLOCode

    Does that mean you want it to do:

    $arr ??= [];
    print_r(array_combine(array_map(fn($b) => $b ?? '', array_column($arr, 'b')), $arr));
  8. clarumedia commented on Nov 3, 2026

    @clarumedia
    Author

    10. Does that mean you want it to do:
    $arr ??= [];
    print_r(array_combine(array_map(fn($b) => $b ?? '', array_column($arr, 'b')), $arr));

    Many thanks for presenting a possible alternative.
    It does use 3 functions to do the same thing as per array_column pre PHP 8.5 but with complex and pretty much unreadable code.
    I'm wondering how it's going to be any more performant than writing a new safe_array_column function which iterates the array in a loop ?

    I never knew that array_column had support for objects as well as per the source code ?
    https://github.com/php/php-src/blob/master/ext/standard/array.c#L4662

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions