在php中匹配数组中的两个元素


Matching two elements in an array in php

我花了一个下午的时间来解决这个问题:

如何检查数组中的{from, to}元素是否相同?换句话说:我需要知道如何在递归函数中匹配数组元素。

这个数组必须返回FALSE,因为$array[4][0]['from']和$array[4][0]['to']在所有$array[2]和$array[3]中不相同。

Array
(
    [4] => Array
        (
            [0] => Array
                (
                    [from] => 1
                    [to] => 2.0000
                    [price] => 8.0000
                )
            [1] => Array
                (
                    [from] => 2.0000
                    [to] => 4.0000
                    [price] => 6.0000
                )
        )
    [2] => Array
        (
            [0] => Array
                (
                    [from] => 1
                    [to] => 3.0000
                    [price] => 70.0000
                )
            [1] => Array
                (
                    [from] => 3.0000
                    [to] => 5.0000
                    [price] => 60.0000
                )
            [2] => Array
                (
                    [from] => 5.0000
                    [to] => 9.0000
                    [price] => 50.0000
                )
        )
    [3] => Array
        (
            [0] => Array
                (
                    [from] => 1
                    [to] => 2.0000
                    [price] => 25.0000
                )
            [1] => Array
                (
                    [from] => 2.0000
                    [to] => 4.0000
                    [price] => 20.0000
                )
            [2] => Array
                (
                    [from] => 4.0000
                    [to] => 6.0000
                    [price] => 15.0000
                )
        )
)

这个数组必须返回TRUE,因为$array[4][0]['from']和$array[4][0]['to']在所有$array[2]和$array[3]中是相同的。

Array
(
    [4] => Array
        (
            [0] => Array
                (
                    [from] => 1
                    [to] => 3.0000
                    [price] => 7.0000
                )
            [1] => Array
                (
                    [from] => 3.0000
                    [to] => 5.0000
                    [price] => 6.0000
                )
            [2] => Array
                (
                    [from] => 5.0000
                    [to] => 9.0000
                    [price] => 5.0000
                )
        )
    [2] => Array
        (
            [0] => Array
                (
                    [from] => 1
                    [to] => 3.0000
                    [price] => 70.0000
                )
            [1] => Array
                (
                    [from] => 3.0000
                    [to] => 5.0000
                    [price] => 60.0000
                )
            [2] => Array
                (
                    [from] => 5.0000
                    [to] => 9.0000
                    [price] => 50.0000
                )
        )
    [3] => Array
        (
            [0] => Array
                (
                    [from] => 1
                    [to] => 3.0000
                    [price] => 170.0000
                )
            [1] => Array
                (
                    [from] => 3.0000
                    [to] => 5.0000
                    [price] => 160.0000
                )
            [2] => Array
                (
                    [from] => 5.0000
                    [to] => 9.0000
                    [price] => 150.0000
                )
        )
)

我想得到的结果只是一个True或False值。

试试这个(没有测试过,这绝对不是最好的方法):

$match = true;
foreach ($main_array as $arrays) 
{
    foreach($arrays as $key => $array)
    {
         $from = $array['from'];
         $to = $array['to'];
         foreach($main_array as $tmp_array)
         {          
             if($tmp_array[$key]['from'] != $from || $tmp_array[$key]['to'] != $to) {               
                $match = false;
                break 3;
             }
         }
    }
}
return $match;

下面是我的例子,我已经测试过了。这里你可以通过可定制字段和可定制的$depth

来比较数组的数组

主要功能:

function array_compare(array $haystack, array $to = array(), array &$matches = array(), $depth = 1)
{
    $total = 0;
    if ($depth === 1) // We're in right depth, let's check the elements
    {
        foreach ($haystack as $key => $value)
        {
            $match = true;
            foreach ($to as $to_key => $to_value) // Check every key
            {
                if (!isset($value[$to_key]) || $value[$to_key] !== $to_value)
                {
                    $match = false; // If at least one doesn't match - break and block adding to $matches
                    break;
                }               
            }
            if ($match)
            {
                $matches[]  = $value; // OK, match, let's add to matches.
            }
            $total++; // Increase total searched elements
        }
    }
    else // We're not on the right depth level
    {
        foreach ($haystack as $key => $value)
        {
            $total += array_compare($value, $to, $matches, $depth - 1); // let's gather into
        }
    }
    return $total; // return total searched
}

如何使用呢:

$test = array(
    0   => array(
        0   => array(
            'from'      => 1,
            'to'        => 3,
            'value'     => 25,
        ),
        1   => array(
            'from'      => 1,
            'to'        => 3,
            'value'     => 25,
        )
    ), 
    1   => array(
        0   => array(
            'from'      => 2,
            'to'        => 5,
            'value'     => 25,
        ),
        1   => array(
            'from'      => 1,
            'to'        => 3,
            'value'     => 25,
        )
    ),
    2   => array(
        1   => array(
            'from'      => 1,
            'to'        => 15,
            'value'     => 25,
        )
    ),
);
$matches    = array();
$total = array_compare($test, array('from' => 1, 'to' => 3), $matches, 2);
var_dump($matches, $total);

如果count($matches) === $total;

希望这正是你需要的;