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条)

报告相同问题?

悬赏问题

  • ¥15 找一个网络防御专家,外包的
  • ¥100 能不能让两张不同的图片md5值一样,(有尝)
  • ¥15 informer代码训练自己的数据集,改参数怎么改
  • ¥15 请看一下,学校实验要求,我需要具体代码
  • ¥50 pc微信3.6.0.18不能登陆 有偿解决问题
  • ¥20 MATLAB绘制两隐函数曲面的交线
  • ¥15 求TYPCE母转母转接头24PIN线路板图
  • ¥100 国外网络搭建,有偿交流
  • ¥15 高价求中通快递查询接口
  • ¥15 解决一个加好友限制问题 或者有好的方案