There is custom function to achieve this,

public function filterArrayByKeys(array $input, array $column_keys)
{
    $result      = array();
    $column_keys = array_flip($column_keys); // getting keys as values
    foreach ($input as val) {
         // getting only those key value pairs, which matches $column_keys
        $result[$key] = array_intersect_key(column_keys);
    }
    return $result;
}

this->filterArrayByKeys($students, ['id','name','email']);
print_r($a);

array_flip — Exchanges all keys with their associated values in an array
array_intersect_key — Computes the intersection of arrays using keys for comparison

Output

Array
(
    [0] => Array
        (
            [id] => 498
            [name] => Andrew A. Blaine
            [email] => [email protected]
        )

    [1] => Array
        (
            [id] => 499
            [name] => Billie C. Heath
            [email] => [email protected]
        )

)

Working demo.

Source.

Answer from Rahul on Stack Overflow
🌐
W3Schools
w3schools.com › php › func_array_column.asp
PHP array_column() Function
MySQL Database MySQL Connect MySQL Create DB MySQL Create Table MySQL Insert Data MySQL Get Last ID MySQL Insert Multiple MySQL Prepared MySQL Select Data MySQL Where MySQL Order By MySQL Delete Data MySQL Update Data MySQL Limit Data · PHP XML Parsers PHP SimpleXML Parser PHP SimpleXML - Get PHP XML Expat Parser PHP DOM Parser · AJAX Intro AJAX PHP AJAX Database AJAX XML AJAX Live Search AJAX Poll ... PHP Examples PHP Compiler PHP Quiz PHP Exercises PHP Practice Problems PHP Server PHP Syllabus PHP Study Plan ... array() array_change_key_case() array_chunk() array_column() array_combine() a
🌐
PHP
php.net › manual › en › function.array-column.php
PHP: array_column - Manual
A multi-dimensional array or an array of objects from which to pull a column of values from. If an array of objects is provided, then public properties can be directly pulled.
🌐
Experts Tool
expertstool.com › combining-multiple-columns-into-one-array-using-array_column-with-php-8-4
Combining Multiple Columns into One Array using array_column() with PHP 8.4 - Experts Tool
January 21, 2025 - You can also use array_column to extract values from multiple columns if you need them together. For example, lets extract both the name and email fields into a new associative array. <?php $namesAndEmails = array_map(function ($user) { return [ 'name' => $user['name'], 'email' => $user['email'], ...
🌐
3D Bay
clouddevs.com › home › php guides › unlocking php’s array_column() function: a practical guide
Unlocking PHP's array_column() Function: A Practical Guide
December 13, 2023 - It allows you to extract a specific column or field from each sub-array within a larger array. This can be incredibly useful when working with data that is organized in a tabular or grid-like structure.
🌐
TheCoolCoder
thecoolcoder.com › 2023 › 08 › php-array_column-function
PHP array_column() Function - TheCoolCoder
August 12, 2023 - You can extract specific columns from a multidimensional array using the PHP array_column() function, which is a strong tool for managing and manipulating data.
🌐
Tutorialspoint
tutorialspoint.com › php › php_function_array_column.htm
PHP array_column() Function
Now let's try one more example to get the column of first names from a recordset but this time we will index recordset using id − · <?php $records = array( array( 'id' => 2135, 'first_name' => 'Zara', 'last_name' => 'Ali', ), array( 'id' => 3245, 'first_name' => 'Nuha', 'last_name' => 'Mac', ), array( 'id' => 5342, 'first_name' => 'Shifa', 'last_name' => 'Alam', ), array( 'id' => 5623, 'first_name' => 'Riya', 'last_name' => 'Sweet', ) ); $first_names = array_column($records, 'first_name', 'id'); print_r($first_names); ?>
🌐
Reintech
reintech.io › blog › a-complete-guide-to-php-array-column-function
A Complete Guide to PHP's `array_column()` Function
April 14, 2023 - When working with database results, API responses, or complex data structures in PHP, you'll frequently need to extract specific values from multi-dimensional arrays. The array_column() function provides an elegant, performant solution for this common task, eliminating the need for manual loops and reducing code complexity.
Find elsewhere
🌐
Educative
educative.io › answers › what-is-the-arraycolumn-method-in-php
What is the array_column() method in PHP?
In PHP, we can use the array_column() method to create a column from a multidimensional array. It will return a single column from an array identified by the column’s key.
🌐
Position Is Everything
positioniseverything.net › home › php array_column: learn to create another array from array column
PHP Array_Column: How to Extract Create Array From Array Column
December 29, 2025 - Certainly, you’ve understood the working of the PHP array_column function. It won’t take much of your time to go through an example to let go of the remaining confusion. Let’s consider this example: you have a multidimensional array named “employees.” You have added multiple arrays inside it, and each inner array stores ids and designations of the employees.
🌐
Plus2Net
plus2net.com › php_tutorial › array_column.php
array_column() to get values from a single column of a multi-dimensional array in PHP
$php_data_array = Array(); $result_set = $connection->query("SELECT * FROM student limit 0,4 ");// Generate resultset while($row = $result_set->fetch_array(MYSQLI_ASSOC)){ //echo $row['id'],$row['name'],$row['class'],$row['mark']."<br>"; $php_data_array[] = $row; // Adding to array } $output = array_column($php_data_array,'name'); echo print_r($output); Output is here
🌐
ZetCode
zetcode.com › php-array › array-column
PHP array_column - Array Column Extraction in PHP
The PHP array_column function extracts a single column from a multi-dimensional array or an array of objects.
🌐
MeshWorld
meshworld.in › tutorials › php › array_column
How to Get Values from Multi-dimensional Arrays with array_column() | MeshWorld
September 1, 2020 - array_column() is an inbuilt PHP function which is used to get the values from a single column from the multi-dimensional array or an array of objects.
🌐
Scaler
scaler.com › home › topics › php array_column() function
PHP array_column() Function - Scaler Topics
April 1, 2024 - The array_column() function in PHP simplifies the extraction of specific columns from multi-dimensional arrays or recordsets.
🌐
Javatpoint
javatpoint.com › post › php-array_column-function
PHP array_column() function - Javatpoint
PHP array_column() Function for beginners and professionals with examples, php tutorial, files, form, functions, regex, diff_ukkey(), values(), key(), column() etc.
🌐
W3training School
w3trainingschool.com › home › articles › use of array_column() and array_combine() functions in php
Use Of Array_column() And Array_combine() Functions In PHP - W3training School
November 11, 2021 - In this write-up, you will be learning the use of array_column() and array_combine() functions in php. The function array_column() is used to output the values from the input array by considering the single column of the input. You can also learn php with deep understanding by joining w3training ...
Top answer
1 of 7
14

If you need two columns from an array where one is SKU (which generally is unique) then you can use array_column with the third parameter.

$new = array_column($arr, "product_id", "product_sku");

This will return a flat array with the SKU as the key and ID as value making the array easy to work with also.

Output:

array(3) {
  [6500722]=>
  string(4) "1138"
  [6501046]=>
  string(4) "1144"
  [6294915]=>
  string(3) "113"
}

https://3v4l.org/UDGiO

2 of 7
6

I think the bigger issue is you lose the keys

Original Code

array (
  0 => 
  array (
    0 => '1138',
    1 => '6500722',
  ),
  1 => 
  array (
    0 => '1144',
    1 => '6501046',
  ),
  2 => 
  array (
    0 => '113',
    1 => '6294915',
 );

You can use a simple foreach instead of the second array_map:

function colsFromArray(array $array, $keys)
{
    if (!is_array($keys)) $keys = [$keys];
    return array_map(function ($el) use ($keys) {
        $o = [];
        foreach($keys as $key){
            //  if(isset($el[$key]))$o[$key] = $el[$key]; //you can do it this way if you don't want to set a default for missing keys.
            $o[$key] = isset($el[$key])?$el[$key]:false;
        }
        return $o;
    }, $array);
}

Output

array (
  0 => 
  array (
    'product_id' => '1138',
    'product_sku' => '6500722',
  ),
  1 => 
  array (
    'product_id' => '1144',
    'product_sku' => '6501046',
  ),
  2 => 
  array (
    'product_id' => '113',
    'product_sku' => '6294915',
  ),
)

Sandbox

the problem is that it seems too laggy, since it iterates twice over this.

There is no real way to not iterate over it 2 times, but you probably don't want to throw away the keys either.

That said you can recursively unset the items you don't want.

function colsFromArray(array &$array, $keys)
{
    if (!is_array($keys)) $keys = [$keys];
    foreach ($array as $key => &$value) {
        if (is_array($value)) {
            colsFromArray($value, $keys); //recursive
        }else if(!in_array($key, $keys)){
           unset($array[$key]); 
        }
    }
}

colsFromArray($array, array("product_id", "product_sku"));
var_export($array);

Same output as before

This is easier to do by reference. Rather or not that is faster you'll have to test the 2 and see.

Sandbox

As a final note you shouldn't assume the key will exist or that keys will be an array unless you type cast it as an array.

You could also do it with array filter

function colsFromArray(array $array, $keys)
{
    if (!is_array($keys)) $keys = [$keys];
    $filter = function($k) use ($keys){
       return in_array($k,$keys);
    };
    return array_map(function ($el) use ($keys,$filter) {
        return array_filter($el, $filter, ARRAY_FILTER_USE_KEY );
    }, $array);
}

There is some small performance benefit to declaring the function for filtering outside of the loop (array_map).

Sandbox

🌐
daily.dev
daily.dev › posts › vvfrq3qik
A Complete Guide to PHP's `array_column()` Function | daily.dev
October 2, 2025 - PHP's array_column() function extracts a single column from multi-dimensional arrays or arrays of objects. The function accepts an input array, a column key to...