1

我得到了以下数组(缩短......多个点意味着它们是那里的数据,两个数组都从条目 1 开始直到结束。

Array
(
    [12U_S_136_15_29_141] => Array
        (
            .....

            [35] => Array
                (
                    [stop_sequence] => 35
                    [stop_id] => 1601394
                )

            .....

            [46] => Array
                (
                    [stop_sequence] => 46
                    [stop_id] => 122052
                )

            [47] => Array
                (
                    [stop_sequence] => 47
                    [stop_id] => 136208
                )

            [48] => Array
                (
                    [stop_sequence] => 48
                    [stop_id] => 128163
                )

        )

    [12U_S_141_57_6_141] => Array
        (
            [1] => Array
                (
                    [stop_sequence] => 1
                    [stop_id] => 1601394
                )

            .....

            [12] => Array
                (
                    [stop_sequence] => 12
                    [stop_id] => 122052
                )

            [13] => Array
                (
                    [stop_sequence] => 13
                    [stop_id] => 136208
                )

            [14] => Array
                (
                    [stop_sequence] => 14
                    [stop_id] => 128163
                )

        )

)

如您所见,两个数组末端相等... 35 = 1, 46 = 12, ..., 48 = 14. 相等,我的意思是相同的,但对于数组条目号stop_id来说总是不同的。stop_sequence

我想知道如何将整个数组与另一个数组进行比较,这样我就可以知道,假设第二个数组是否以 100% 匹配第一个数组(除非我们不寻找,stop_sequence所以这可能会有所不同。所以在在这种情况下,两者都将被标记为“相等”,但如果假设最后一个条目具有不同的 stop_id(条目 48 将是条目 14 的 !=),则数组将被标记为“不等于”。

有没有人可以引导我?我一直在想,但不知道怎么做。我试过 array_compare 但这没有任何结果:\

谢谢

编辑 不能对数据库进行任何更改。此外,需要以这种方式创建数组(带有奇怪的文本,即12U_S_136_15_29_141)。我可以在 PHP 中做任何事情。

4

1 回答 1

0

将 stop_id 提取到新数组:s 并进行比较

这是一个有用的功能

    function sub_array($array, $sub_key)
    {
            $new_array = array();
            if(is_array($array) AND $array)
            {
                    foreach($array as $key => $data)
                    {
                            $new_array[$key] = $data[$sub_key];
                    }
            }
            return $new_array;
    }

然后只比较数组

if(sub_array($a['12U_S_136_15_29_141'], 'stop_id') == sub_array($a['12U_S_141_57_6_141'], 'stop_id'))

或者,如果您知道匹配的 stop_id:

$first_stop_id_list = sub_array($a['12U_S_136_15_29_141'], 'stop_id');
$secound_stop_id_list = sub_array($a['12U_S_141_57_6_141'], 'stop_id');
$matches = array_intersect($first_stop_id_list, $secound_stop_id_list);

if(!$matches)
   echo "No stop_id matches";
else if($first_stop_id_list = $secound_stop_id_list)
   echo "all element was in both";
else if($matches == $first_stop_id_list)
   echo "all element in first was in secound";
else if($matches == $secound_stop_id_list)
   echo "all element in secound was in first";
于 2012-06-28T17:50:55.603 回答