note 102520 added to function.preg-match-all

[email protected] Fri, 18 Feb 2011 11:21: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.

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
        )
)
----
Server IP: 69.147.83.197
Probable Submitter: 70.181.209.72
----
Manual Page -- http://www.php.net/manual/en/function.preg-match-all.php
Edit        -- https://master.php.net/note/edit/102520
Del: integrated  -- https://master.php.net/note/delete/102520/integrated
Del: useless     -- https://master.php.net/note/delete/102520/useless
Del: bad code    -- https://master.php.net/note/delete/102520/bad+code
Del: spam        -- https://master.php.net/note/delete/102520/spam
Del: non-english -- https://master.php.net/note/delete/102520/non-english
Del: in docs     -- https://master.php.net/note/delete/102520/in+docs
Del: other reasons-- https://master.php.net/note/delete/102520
Reject      -- https://master.php.net/note/reject/102520
Search      -- https://master.php.net/manage/user-notes.php