Are there any specific PHP functions or libraries that can assist with organizing data in a spiral format?

Organizing data in a spiral format involves arranging data in a spiral pattern, typically starting from the center and spiraling outwards. One way to achieve this is by using nested loops to iterate through the data in a spiral order.

function spiralOrder($matrix) {
    $result = [];
    $rowStart = 0;
    $rowEnd = count($matrix) - 1;
    $colStart = 0;
    $colEnd = count($matrix[0]) - 1;
    
    while ($rowStart <= $rowEnd && $colStart <= $colEnd) {
        for ($i = $colStart; $i <= $colEnd; $i++) {
            $result[] = $matrix[$rowStart][$i];
        }
        $rowStart++;

        for ($i = $rowStart; $i <= $rowEnd; $i++) {
            $result[] = $matrix[$i][$colEnd];
        }
        $colEnd--;

        if ($rowStart <= $rowEnd) {
            for ($i = $colEnd; $i >= $colStart; $i--) {
                $result[] = $matrix[$rowEnd][$i];
            }
            $rowEnd--;
        }

        if ($colStart <= $colEnd) {
            for ($i = $rowEnd; $i >= $rowStart; $i--) {
                $result[] = $matrix[$i][$colStart];
            }
            $colStart++;
        }
    }
    
    return $result;
}

$matrix = [
    [1, 2, 3],
    [4, 5, 6],
    [7, 8, 9]
];

$result = spiralOrder($matrix);
print_r($result);