I’m building a platform. Somewhere in my code, there’s an array that looks like this (PHP):
$entries = array('p01','p02','g01','g02','a001','a002')
I need to write a script that filters the array based on the first letter. For example, asking for those with the starting letter “p” would give me
$filtered_entries = array('p01','p02');
Similarly, if I asked for those with starting letter “g” or “a” it would give me those as well. Any idea how to accomplish this?
There is an
array_filter()function in PHP which you can use to accomplish this:$filtered = array_filter($array, create_function('$a', 'return $a[0] == "' . $letter . '";'));I’ll leave it to you to generalize the function to handle all the letters.See: http://www.php.net/manual/en/function.array-filter.php