[ create a new paste ] login | about

Link: http://codepad.org/JCdsAMGk    [ raw code | output | fork ]

PHP, pasted on Jul 10:
<?php
function array_pattern($array, $pattern){
    $ret = array(array());
    $found = true;
    foreach($pattern as $val){
        $x = array_search($val, $array);
        if($x === FALSE){
            $found = FALSE;
            break;
        }
        unset($array[$x]);
        $ret[0][] = $x;
    }
    return $found ? array_merge($ret, array_pattern($array, $pattern)) : array();
}

$a = array_pattern(array(0 => 'A', 1 => 'A', 2 => 'B', 3 => 'B', 4 => 'B'), array('A', 'B'));
$b = array_pattern(array(0 => 'F', 5 => 'G', 78 => 'R', 2 => 'D'), array('G', 'R', 'F'));

echo "Example 1\n";
print_r($a);

echo "\nExample 2\n";
print_r($b);


Output:
Example 1
Array
(
    [0] => Array
        (
            [0] => 0
            [1] => 2
        )

    [1] => Array
        (
            [0] => 1
            [1] => 3
        )

)

Example 2
Array
(
    [0] => Array
        (
            [0] => 5
            [1] => 78
            [2] => 0
        )

)


Create a new paste based on this one


Comments: