douquanzhan0315 2014-08-27 02:36
浏览 20
已采纳

PHP比较两个数组之间的特定字符

In PHP, I have array like this:

Array
(
    [0] => 09:26-DOWN
    [1] => 09:26-UP
    [2] => 09:26-UP
    [3] => 09:26-UP
    [4] => 09:26-UP
    [5] => 09:26-UP
    [6] => 09:26-UP
    [7] => 09:26-UP
    [8] => 09:26-DOWN
    [9] => 09:26-UP
    [10] => 09:26-UP
    [11] => 09:26-UP
    [12] => 09:26-UP
    [13] => 09:26-UP
    [14] => 09:26-UP
    [15] => 09:26-UP
    [16] => 09:26-UP
    [17] => 09:26-UP
)

and I want to compare it with below array, only with the last characters (UP and DOWN):

Array
(
    [0] => 09:31-DOWN
    [1] => 09:31-UP
    [2] => 09:31-UP
    [3] => 09:31-UP
    [4] => 09:31-UP
    [5] => 09:31-UP
    [6] => 09:31-UP
    [7] => 09:31-UP
    [8] => 09:31-UP
    [9] => 09:31-UP
    [10] => 09:31-UP
    [11] => 09:31-UP
    [12] => 09:31-UP
    [13] => 09:31-UP
    [14] => 09:31-UP
    [15] => 09:31-UP
    [16] => 09:31-UP
    [17] => 09:31-UP
)

So the purpose is to check if it up or down between the specific time.

  • 写回答

3条回答 默认 最新

  • 普通网友 2014-08-27 03:07
    关注

    Not exactly sure what you're trying to do, but here's a simple option. Assuming that the format of each element will always be the same, ie HH:II-[UP or DOWN], you could do this:

    $result = array();
    foreach ($arr1 as $k=>$v) {
        $result[] = $v[6] . $arr2[$k][6];
    }
    

    Given your two arrays, $arr1 and $arr2, this will yield a third array like:

    Array
    (
        [0] => DD
        [1] => UU
        [2] => UU
        [3] => UU
        [4] => UU
        ....
    )
    

    Then you can use this array's values, UU, DD, UD, DU, to figure out what is in each position. For example key 3 will have UU, so you know it is UP, UP....

    See demo


    Update Here's the one-liner version using array_map():

    $result = array_map(function($a, $b){return $a[6] . $b[6];}, $arr1, $arr2);
    

    See demo 2

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作