dongwh1992 2017-03-30 06:49
浏览 89
已采纳

如何在两个指定键之间提取一系列元素?

I have two arrays, the first one is:

Array (
  [0] => Mar
  [1] => Jun
)

and the second one is:

Array (
  [Jan] => January
  [Feb] => February
  [Mar] => March
  [Apr] => April
  [May] => May
  [Jun] => June
  [Jul] => July 
  [Aug] => August
  [Sep] => September
  [Oct] => October
  [Nov] => November
  [Dec] => December
)

I'd like to extract the elements from the first nominated month to the second nominated month in the search array.

My expected result is:

Array (
  [Mar] => March
  [Apr] => April
  [May] => May
  [Jun] => June
)
  • 写回答

2条回答 默认 最新

  • doudou1897 2017-03-30 07:02
    关注

    Please check below answer, May be it will help you:

    $fullArray = [
        'Jan' => 'January',
        'Feb' => 'February',
        'Mar' => 'March',
        'Apr' => 'April',
        'May' => 'May',
        'Jun' => 'June',
        'Jul' => 'July',
        'Aug' => 'August',
        'Sep' => 'September',
        'Oct' => 'October',
        'Nov' => 'November',
        'Dec' => 'December',
    ];
    
    
    $arrayToCompare = [
        'Mar', 'Jun'
    ];
    
    
    
    $matchedArray = array();
    
    $matchedFirst = false;
    $matchedLast = false;
    
    foreach ($fullArray as $key => $value) {
        if ($key == $arrayToCompare[0]) {
            $matchedFirst = true;
        }
    
        if ($key == $arrayToCompare[1]) {
            $matchedLast = true;
        }
    
        if ($matchedFirst == true) {
            $matchedArray[$key] = $value;
        }
    
        if ($matchedLast == true) {
            $matchedArray[$key] = $value;
            break;
        }
    
    }
    
    print_r($matchedArray);
    die;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 simulink单相桥式整流电路
  • ¥35 问问51单片机流水灯的代码该怎么写
  • ¥15 关于#百度#的问题:感觉已经将字体段落、字体、页边距、纸张大小、文档网络调成与论文模板一致,为什么黄色部分字体左右的间距还是不一样啊,求私信发文件接收看一下
  • ¥15 stata webuse报错
  • ¥15 TypeError: Cannot read properties of undefined (reading 'status')
  • ¥15 如何利用AI去除图片中的竹架子
  • ¥15 python 写个基金爬取的代码,自动卖出功能
  • ¥15 Linux系统启动不起来
  • ¥15 为什么运行仿真数码管不亮(语言-c语言)
  • ¥15 陈仁良《直升机飞行动力学》小扰动线化方程如何推导