What is the best way to copy a column from one array to another in PHP?

To copy a column from one array to another in PHP, you can use a loop to iterate over the original array and extract the values from the desired column into a new array. This can be achieved by accessing the specific column index for each row in the original array and adding it to the new array.

// Original array
$originalArray = [
    [1, 'John', 25],
    [2, 'Jane', 30],
    [3, 'Alice', 28]
];

// New array to store the copied column
$newArray = [];

// Column index to copy (e.g., index 1 for names)
$columnIndex = 1;

// Copy the specified column to the new array
foreach ($originalArray as $row) {
    $newArray[] = $row[$columnIndex];
}

// Output the copied column
print_r($newArray);