获取数组中的第一个字母

I have an array and I'm trying to get only the names that begin with "L" or "l" to be echoed out.

$array = array('Leanna', 'nicole', 'Lisa', 'Sarah', 'Leopard', 'Michael', 'jack', 'logan');

foreach($array as $key => $value){
    $exp_key = explode('L', $key);
    if($exp_key[0] == 'L' || $exp_key[0] == "l"){
        $arr_result[] = $value;
    }

    echo $value . "<br />";
}

but I still get all the values

You need to use $value and don't need to explode(). And you were echoing $value which will give you all values, you rather need to use $arr_result variable:

<?php
$array = array('Leanna', 'nicole', 'Lisa', 'Sarah', 'Leopard', 'Michael', 'jack', 'logan');

foreach($array as $key => $value){
    if($value[0] == 'L' || $value[0] == "l"){
        $arr_result[] = $value;
    }
}

print_r($arr_result);

?>

Output:

Array
(
    [0] => Leanna
    [1] => Lisa
    [2] => Leopard
    [3] => logan
)

You can use the buildt in function array_map.

array_map — Applies the callback to the elements of the given Arrays

$array = array('Leanna', 'nicole', 'Lisa', 'Sarah', 'Leopard', 'Michael', 'jack', 'logan');


array_map("getEntries", $array);

function getEntries($e) {
    if($e[0] == "L" || $e[0] == "l") {
        echo $e;
    }
}

explode('l',$arr) removes l from the resulting array

$arr_result=[];
$array = array('Leanna', 'nicole', 'Lisa', 'Sarah', 'Leopard', 'Michael', 'jack', 'logan');
foreach($array as $key => $value){
    if($value[0] == 'L' || $value[0] == "l"){
        $arr_result[] = $value;
    }

    echo $value . "<br />";
}

print_r($arr_result);
$array = array('Leanna', 'nicole', 'Lisa', 'Sarah', 'Leopard', 'Michael', 'jack', 'logan');

foreach($array as $value){
    $lettersArray = explode('', $value);
    if($lettersArray[0] == 'L' || $lettersArray[0] == "l"){
        $arr_result[] = $value;
        // echo $value . "<br />";  # to echo names as you go
    }
}

print_r ($result) // if you want to print all the values as an array at the end

Try this. But i think there should be a more simple way as well :D

The thing wrong with your code is that you echo every value in every iteration in the cycle and only add it to array if it is correct.

What's wrong with your code?

This is the code you posted in the question:

$array = array('Leanna', 'nicole', 'Lisa', 'Sarah', 'Leopard', 'Michael', 'jack', 'logan');

foreach($array as $key => $value){
    $exp_key = explode('L', $key);
    if($exp_key[0] == 'L' || $exp_key[0] == "l"){
        $arr_result[] = $value;
    }

    echo $value . "<br />";
}

How it works?

On the first iteration, $key is set to number 0 and $value is the string 'Leanna'.

The statement $exp_key = explode('L', $key); converts $key to the string '0' and splits it into an array of strings using the string 'L' as separator. Since there is no L inside 0, the result is an array that contains only one element: the input string (array(0 => '0')). Consequently, $exp_key[0] is the string '0'.

According to the table of loose comparisons using ==, the string '0' is not equal to neither 'L' nor 'l' and the statement inside the if block doesn't execute.

The echo $value statement, however, is executed and it prints Leanna<br />.

On the next loop, $key is the number 1 and $value is the string 'nicole'. The processing works similar to the first iteration, the comparisons produce the same result (false), nothing is stored in $arr_result and $value is printed. The same happens again and again until all the elements of $array are processed.

How to make it work

First, you shouldn't care about the keys of $array. They are consecutive integer numbers starting with 0 and they are not useful in any way for the goal.

Then, you should compare $value[0] against uppercase and lowercase L. There is no need to explode() anything, all you care about is the first character of each value.

The code could look like this:

$array = array('Leanna', 'nicole', 'Lisa', 'Sarah', 'Leopard', 'Michael', 'jack', 'logan');

// Make sure $arr_result is always an array
// Cannot rely on the assignment inside the loop to initialize it because 
// if no value in $array starts with 'L', the statement $arr_result[] = $value
// is never executed and at the end $arr_result is undefined.
$arr_result = array();

foreach ($array as $value) {
    if ($value[0] == 'L' || $value[0] == 'l'){
        $arr_result[] = $value;
    }
}

print_r($arr_result);

The output is:

Array
(
    [0] => Leanna
    [1] => Lisa
    [2] => Leopard
    [3] => logan
)

Alternative solutions

You can use the PHP function array_filter() to extract from $array only the matching values:

$arr_result = array_filter(
    $array,
    function ($value) {
        return strtoupper($value[0]) == 'L';
    }
);

This solution is slightly slower than manually looping through the array but it is more clear. The anonymous function used as callback does only the comparison (the filtering) and the assignment to $arr_result happens only once, when array_filter() returns. There is no need to initialize $arr_result any more because array_filter() always returns an array.