PHP array_column - how to keep the keys?

laukok picture laukok · Nov 29, 2014 · Viewed 15.5k times · Source
    $items = array(
        1 => [
            "id" => 5
        ],

        3 => [
            "id" => 6
        ],
        4 => [
            "id" => 7
        ],
    );

    var_dump(array_column($items,"id"));

result,

array (size=3)
  0 => int 5
  1 => int 6
  2 => int 7

But how can I keep the key of $items so I can get this below?

array (size=3)
  1 => int 5
  3 => int 6
  4 => int 7

Answer

Ashish Choudhary picture Ashish Choudhary · Jun 30, 2015

See if this could help

array_filter(array_combine(array_keys($items), array_column($items, 'id')));