1

I'm using php, and it is pretty much the same as this C++ question: link I'm wondering if there is an easier way to do it than making a function.

Example code

array[9] = 1;
array[5] = 1;
array[14] = 1;

array[8] = 2;
array[15] = 2;
array[23] = 2;
array[12] = 2;

Edit : by easier I mean less tedious and less lines

Community
  • 1
  • 1
David Mckee
  • 1,100
  • 3
  • 19
  • 35
  • 1
    Notice: "easier" is undefined in question on line 1 – steven Nov 18 '13 at 20:56
  • Generally speaking, to have exactly this array, you need to code it manually. But there are some functions that may be interesting to you: http://us2.php.net/manual/en/function.array-fill.php and http://us2.php.net/manual/en/function.range.php. – Alex Nov 18 '13 at 20:56

1 Answers1

2

you can use PHP 5.4+ array_fill_keys()

$a = array_fill_keys([9,5,14], 1);
$b = array_fill_keys([8,15,23,12], 2);

PHP 5.2+ (thanks jszobody)

$a = array_fill_keys(array(9,5,14), 1);
$b = array_fill_keys(array(8,15,23,12), 2);
liam.derossi
  • 106
  • 1
  • 3