0

i am trying to create function to check if array element is the last array element in one array. Original array looks like:

array(1001,
      1002,
      array('exam'=>true, 'index'=>10),
      1003,
      1004,
      1005,
      array('exam'=>true, 'index'=>20),
      1006,
      1007,
      array('exam'=>true, 'index'=>30),
      1008,
      1009
)

I this case to prove if "array('exam'=>true, 'index'=>30)" is the last.

I have index position of that element, but I do not know how to check if that is the last array element.

    function is_last_exam_in_survey($array, $exam_position) {

      foreach($array as $element) {
        if(!is_numeric($element) {
          // check if that is the last array element in array

          //return true;
        } else {
          // return false;
        }
      }

    }

I would be grateful for any advice:)

lukpar
  • 27
  • 5
  • Does this answer your question? [What's the best way to get the last element of an array without deleting it?](https://stackoverflow.com/questions/3687358/whats-the-best-way-to-get-the-last-element-of-an-array-without-deleting-it) – Bananaapple Apr 20 '20 at 10:54

6 Answers6

0
function get_last_exam_in_survey($array) {
  $last = null;
  foreach($array as $element) {
    if(is_array($element) && !empty($element['exam'])) {
      $last = $element;
    }
  }
  return $last;
}


function is_last_exam_in_survey($array, $exam_position) {
  $last_exam = get_last_exam_in_survey($array);
  return !empty($last_exam) && ($last_exam['index']==$exam_position);
}
Nikos M.
  • 8,033
  • 4
  • 36
  • 43
0

I think this would be the quickest solution:

function is_last_exam_in_survey($array, $exam_position) {
    $last_index = array_key_last( $array );

    if( $exam_position == $last_index ){
        return true;
    }else{
        return false;
    }

}

You can still change the conditional statement if you are trying to compare values from the last element, for example:

if( isset($last_index['index']) && $exam_position == $last_index['index'] ){

Also, if you want to get the latest array value instead of key, you could use this:

$last_index = end( $array );
Ali_k
  • 1,642
  • 1
  • 11
  • 20
  • i think he wants to FIND what is the last exam wherever it might be, NOT check if the last element is an exam – Nikos M. Apr 20 '20 at 11:00
0

I would reverse the array, and look for the first element. Something like:

function is_last_exam_in_survey($array, $exam_position) {   
  foreach(array_reverse($array) as $element) {
    if(!is_numeric($element) {
      return $element['index'] === $exam_position;
    }
  }
  return false;  
}

Seems like the most efficient and simplest solution to me.

Ben Hillier
  • 2,126
  • 1
  • 10
  • 15
  • it is not more efficient than checking in forward direction, as `array_reverse` still has O(N) time complexity – Nikos M. Apr 20 '20 at 11:03
0

this solution avoid loop. at first we find out the last index of array.Then we use is_array() function for check the last element is array or not.

function get_last_exam_in_survey(array $arr)
{
  $lastOfset = count($arr) - 1;
  if(is_array($arr[$lastOfset])){
    return true;
  }else{
    return false;
  }
}
Anik Anwar
  • 625
  • 5
  • 11
0

I think you can use array_column function to do that

function is_last_exam_in_survey($array,$exam_position){

    $ac = array_column($array, 'index'); // return array(10,20,30) 
    $la = $ac[count($ac) - 1]; // 30 

    foreach ($array as $element) {
        if (!is_numeric($element)) {
            // check if $element['index'] === 30
            if($element['index'] === $la){
                return true;
            }
        }
    }
}
Diyata
  • 43
  • 7
0

How about using array_slice to extract the values in the array that are after the position you are looking at, then array_filter to remove any values that are not arrays. If there are any values left, then the entry you are looking at is not the last array entry in the array.

This may not be very efficient if you are calling it a lot of times with a large array. It may be better to rethink the way the data is stored or loaded into the array.

function is_last_exam_in_survey($array, $exam_position)
{
    return isset($array[$exam_position])
        && is_array($array[$exam_position])
        && !array_filter(array_slice($array, $exam_position + 1), 'is_array');
}
Matt Raines
  • 4,149
  • 8
  • 31
  • 34