note 102520 modified in function.preg-match-all by danbrown

[email protected] Fri, 18 Feb 2011 11:25:42 -0800
Newsgroups php.notes
Message-ID <[email protected]>
I needed a function to rotate the results of a preg_match_all query, and made this. Not sure if it exists.

<?php
function turn_array($m)
{
    for ($z = 0;$z < count($m);$z++)
    {
        for ($x = 0;$x < count($m[$z]);$x++)
        {
            $rt[$x][$z] = $m[$z][$x];
        }
    }    
    
    return $rt;
}
?>

Example - Take results of some preg_match_all query:

Array
(
    [0] => Array
        (
            [1] => Banff 
            [2] => Canmore
            [3] => Invermere
        )
 
    [1] => Array
        (
            [1] => AB 
            [2] => AB
            [3] => BC
        )
 
    [2] => Array
        (
            [1] => 51.1746254 
            [2] => 51.0938416
            [3] => 50.5065193
        )
 
    [3] => Array
        (
            [1] => -115.5719757 
            [2] => -115.3517761
            [3] => -116.0321884
        )
 
    [4] => Array
        (
            [1] => T1L 1B3 
            [2] => T1W 1N2
            [3] => V0B 2G0
        )

)

Rotate it 90 degrees to group results as records:

Array
(
    [0] => Array
        (
            [1] => Banff 
            [2] => AB
            [3] => 51.1746254
            [4] => -115.5719757
            [5] => T1L 1B3
        )
 
    [1] => Array
        (
            [1] => Canmore
            [2] => AB
            [3] => 51.0938416
            [4] => -115.3517761
            [5] => T1W 1N2
        )
 
    [2] => Array
        (
            [1] => Invermere
            [2] => BC
            [3] => 50.5065193
            [4] => -116.0321884
            [5] => V0B 2G0
        )
)

--was--
I needed a function to rotate the results of a preg_match_all query, and made this. Not sure if it exists.

function turn_array($m)
{
    for ($z = 0;$z < count($m);$z++)
    {
        for ($x = 0;$x < count($m[$z]);$x++)
        {
            $rt[$x][$z] = $m[$z][$x];
        }
    }    
    
    return $rt;
}

Example - Take results of some preg_match_all query:

Array
(
    [0] => Array
        (
            [1] => Banff 
            [2] => Canmore
            [3] => Invermere
        )
 
    [1] => Array
        (
            [1] => AB 
            [2] => AB
            [3] => BC
        )
 
    [2] => Array
        (
            [1] => 51.1746254 
            [2] => 51.0938416
            [3] => 50.5065193
        )
 
    [3] => Array
        (
            [1] => -115.5719757 
            [2] => -115.3517761
            [3] => -116.0321884
        )
 
    [4] => Array
        (
            [1] => T1L 1B3 
            [2] => T1W 1N2
            [3] => V0B 2G0
        )

)

Rotate it 90 degrees to group results as records:

Array
(
    [0] => Array
        (
            [1] => Banff 
            [2] => AB
            [3] => 51.1746254
            [4] => -115.5719757
            [5] => T1L 1B3
        )
 
    [1] => Array
        (
            [1] => Canmore
            [2] => AB
            [3] => 51.0938416
            [4] => -115.3517761
            [5] => T1W 1N2
        )
 
    [2] => Array
        (
            [1] => Invermere
            [2] => BC
            [3] => 50.5065193
            [4] => -116.0321884
            [5] => V0B 2G0
        )
)

http://php.net/manual/en/function.preg-match-all.php