Make simple arrays in PHP
Howto, php February 7th, 2008I always liked the qw function from perl. You could easily make an array just by seperating the values with whitespaces. For example:
1 2 | #!usr/bin/perl $array = qw("first second third fourth"); |
You would now have an array consisting of the values ‘first’, ’second’, ‘third’ and ‘fourth’. in PHP you would have to type the following code.
1 2 3 | <?php $array = array('first', 'second', 'third', 'fourth'); ?> |
This is much longer than the perl function.. So I simply made the qw function for PHP. It was actually very simple.
1 2 3 4 5 6 7 | <?php function qw($string) { $array = explode(' ', $string); return $array; } ?> |
Sample use:
1 2 3 4 | <?php $array = qw('first second third fourth fifth'); print_r($array); ?> |
Output:
Array
(
[0] => first
[1] => second
[2] => third
[3] => fourth
[4] => fifth
)
Recent Comments