Q:

Write a PHP program to get the index of the last element for which the given function returns a truth value

0

Write a PHP program to get the index of the last element for which the given function returns a truth value.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

<?php
function find_last_Index($items, $func)
{
    $keys = array_keys(array_filter($items, $func));

    return array_pop($keys);
}

echo find_last_Index([1, 2, 3, 4], function ($n) {
    return ($n % 2) === 1;
});

echo "\n";
echo find_last_Index([1, 2, 3, 4], function ($n) {
    return ($n % 2) === 0;
});
?>

Sample Output:

2
3

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now