1

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
)
4
  • 1
    Start with foreach
    – u_mulder
    Commented Mar 30, 2017 at 6:50
  • You can use built-in function in_array() Commented Mar 30, 2017 at 6:52
  • What's the need of the first array? Your question is not clear
    – Red Bottle
    Commented Mar 30, 2017 at 6:54
  • Your second array is same as you want in output ... Commented Mar 30, 2017 at 6:56

2 Answers 2

3

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;
0
1

A concise solution can be written much more elegantly without foreach looping with conditionals.
I'll offer a couple of clean methods leveraging just four array functions calls each.

Input:

$months=[
    'Jan' => 'January',
    'Feb' => 'February',
    'Mar' => 'March',
    'Apr' => 'April',
    'May' => 'May',
    'Jun' => 'June',
    'Jul' => 'July',
    'Aug' => 'August',
    'Sep' => 'September',
    'Oct' => 'October',
    'Nov' => 'November',
    'Dec' => 'December'
];
$range=['Mar','Jun'];

Method #1: (just two lines of code and four functions) Demo

// create indexed array of keys in $month, retain only 2 selected months, get their indexes
$a=array_keys(array_intersect(array_keys($months),$range));
// slice the desired elements using offset and calculated length
var_export(array_slice($months,$a[0],$a[1]-$a[0]+1));

Method #2 (just four lines of code and four functions) Demo

$indexes=array_keys($months);  // generate an indexed array of abbreviated months
$start=array_search($range[0],$indexes);  // get the index of the starting month
$length=array_search($range[1],$indexes)-$start;  // subtract start from end month index
var_export(array_slice($months,$start,$length+1));  // extract the desired elements

Output: (using either method)

array (
  'Mar' => 'March',
  'Apr' => 'April',
  'May' => 'May',
  'Jun' => 'June',
)
1
  • Really appreciate your answer @mickmackusa Commented Jun 20, 2017 at 5:44

Not the answer you're looking for? Browse other questions tagged or ask your own question.