What are some best practices for manipulating multidimensional arrays in PHP, especially when needing to move specific rows based on certain criteria?

When manipulating multidimensional arrays in PHP and needing to move specific rows based on certain criteria, one approach is to iterate through the array, identify the rows that meet the criteria, and then rearrange them accordingly. This can be achieved by creating a new array to hold the rows that need to be moved, removing them from the original array, and then inserting them back at the desired position.

// Sample multidimensional array
$multiArray = [
    ['id' => 1, 'name' => 'John'],
    ['id' => 2, 'name' => 'Jane'],
    ['id' => 3, 'name' => 'Alice'],
    ['id' => 4, 'name' => 'Bob'],
];

// Criteria for moving rows (e.g., move rows with id greater than 2 to the beginning)
$rowsToMove = [];
foreach ($multiArray as $key => $row) {
    if ($row['id'] > 2) {
        $rowsToMove[] = $row;
        unset($multiArray[$key]);
    }
}

// Insert the moved rows at the beginning of the array
$multiArray = array_merge($rowsToMove, $multiArray);

// Output the updated multidimensional array
print_r($multiArray);