Split array into parts based on matching values

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
cj5
Forum Commoner
Posts: 60
Joined: Tue Jan 17, 2006 3:38 pm
Location: Long Island, NY, USA

Split array into parts based on matching values

Post by cj5 »

I have a loop that builds a giant array of values. This array is actually a series of parts; in other words, I need to extract the parts based on an initial value, which somewhere along the way will match another value. When I hit that second match, I need to extract that series of iterations into it's own array, and do the same to the next, and so on...

Example:

This...

Code: Select all

Array
(
    [0] => Array
        (
            [val1] => 20.5
            [val2] => 39
        )

    [1] => Array
        (
            [val1] => 21
            [val2] => 35
        )

    [2] => Array
        (
            [val1] => 20
            [val2] => 45
        )

    [3] => Array
        (
            [val1] => 20.5
            [val2] => 39
        )

    [4] => Array
        (
            [val1] => 42
            [val2] => 39.1
        )

    [5] => Array
        (
            [val1] => 23
            [val2] => 37
        )

    [6] => Array
        (
            [val1] => 56
            [val2] => 35
        )

    [7] => Array
        (
            [val1] => 42
            [val2] => 39.1
        )

)
to this...

Code: Select all

Array
(
    [0] => Array
        (
            [0] => Array
                (
                    [val1] => 20.5
                    [val2] => 39
                )

            [1] => Array
                (
                    [val1] => 21
                    [val2] => 35
                )

            [2] => Array
                (
                    [val1] => 20
                    [val2] => 45
                )

            [3] => Array
                (
                    [val1] => 20.5
                    [val2] => 39
                )

        )

    [1] => Array
        (
            [0] => Array
                (
                    [val1] => 42
                    [val2] => 39.1
                )

            [1] => Array
                (
                    [val1] => 23
                    [val2] => 37
                )

            [2] => Array
                (
                    [val1] => 56
                    [val2] => 35
                )

            [3] => Array
                (
                    [val1] => 42
                    [val2] => 39.1
                )

        )

)
How can I accomplish this? I'm banging my head trying to grasp the iteration logic.

Cheers!
User avatar
cj5
Forum Commoner
Posts: 60
Joined: Tue Jan 17, 2006 3:38 pm
Location: Long Island, NY, USA

Post by cj5 »

Nevermind I solved it! :lol:

Code: Select all

$newArray = array();
$oaStep = 0;
$innerStep = 0;
$matchStep = 0;

for($a=0; $a<count($originalArray); $a++) {
    $newArray[$oaStep][$innerStep] = $originalArray[$a];

    if(
       $originalArray[$a]['X'] == $originalArray[$matchStep]['X'] && 
       $originalArray[$a]['Y'] == $originalArray[$matchStep]['Y']
      ) {
        if($a > $matchStep) {
            $matchStep = $a + 1;
            $oaStep++;
            $innerStep = 0;
        }
    }

    $innerStep++;
}
Post Reply