【问题标题】:Matching two elements in an array in php在php中匹配数组中的两个元素
【发布时间】:2012-01-02 20:06:51
【问题描述】:

我花了一个下午试图解决这个问题:

如何检查此数组中的 {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 值作为结果。

【问题讨论】:

  • 也许尝试用不同的方式来表达你的问题。我真的不明白你在寻找什么......
  • 嗨@DaveRandom我已经更新了这个问题。谢谢

标签: php arrays match matching


【解决方案1】:

试试这个(未经测试,绝对不是最好的方法):

$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;

【讨论】:

  • 我刚刚测试了它,它有效,如果它适合你,请告诉我。
  • 嗨@redmoon7777 我已经尝试过你的代码,但在我看来它不起作用,我可能错了。你能告诉我在哪里吗? codepad.org/bYA9b3gG 谢谢
  • 你的错误在第 18 行,应该是 foreach($ranges as $tmp_array) 而不是 foreach($arrays as $tmp_array)
【解决方案2】:

这是我的例子,我已经测试过了。在这里,您可以通过可自定义的字段和可自定义的 $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);

你的真实情况必须是 if count($matches) === $total;

希望这正是您所需要的;

【讨论】:

    猜你喜欢
    • 2018-09-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多