Monday, 2 February 2015

Combine two arrays

Combine two arrays

This example shows how to combine two arrays
<?php
  
// an array of months by name
  
$months = array(
  
'January',
  
'February',
  
'March',
  
'April',
  
'May',
  
'June',
  
'July',
  
'August',
  
'September',
  
'October',
  
'November',
  
'December');
  
// an array of numbers combined with the months array
  
$blah array_combine(range(1,12), $months);

  
// loop through the resulting array
  
foreach($blah as $k=>$v){ echo $k.' -> '.$v.'<br />'; }
?>
Will produce an ouput like this...
1 - January
2 - February
3 - March
4 - April
5 - May
6 - June
7 - July
8 - August
9 - September
10 - October
11 - November
12 - December

0 comments:

Post a Comment