-1

was wondering if there's a function to search an array, where the first letter matches a letter chosen. I could do something less elegant like

Loop through array 
Each item remove the first character, match to chosen search variable   e.g if the first letter from apple, a equals my selection a, show. 
ChipsLetten
  • 2,923
  • 1
  • 11
  • 28
GAV
  • 1,205
  • 2
  • 18
  • 38

1 Answers1

1

Your question is not clear. But below I give you an example of selecting only the selected elements of an array that contain the first letter you want:

function select_from_array($first_letter,$array){
    $return = Array();
    for($i=0;$i<count($array);$i++){
        if($array[$i][0] === $first_letter) $return[] = $array[$i];
    }
    return $return;
}

Example:

$arr = Array("Nice","Chops","Plot","Club");
print_r(select_from_array('C',$arr));

Result:

Array
(
    [0] => Chops
    [1] => Club
) 
Muntashir Akon
  • 8,740
  • 2
  • 27
  • 38