0

I have a multi-select form feeding from database, if I implode it with comma separated, I will get values like item1,item2,item3. However, I only want to get the last selected item which is item3 in d case above. I also tried to explode to split it but didn't work out

this is my code below

<select multiple="multiple" name="enrolledterm[]" style="width: 100%;" data-toggle="select2" data-placeholder="Select Terms.." data-allow-clear="true" required="required">
            <option value="<?php echo $term_one_date ?>">Winter Term ends on <?php echo $term_one_date ?></option>
            <option value="<?php echo $term_two_date ?>">Spring Term ends on <?php echo $term_two_date ?></option>
            <option value="<?php echo $term_three_date ?>">Summer Term ends on <?php echo $term_three_date ?></option>
                </select>

$enrolled_term = explode(" ", $_POST['enrolled_term']) ;
Zakir hussain
  • 621
  • 3
  • 12
Ademaintain
  • 137
  • 2
  • 13
  • Possible duplicate of [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) – mickmackusa Jul 31 '17 at 05:24

5 Answers5

2

You can try this simplest one, Here we are using end function.

Try this with an example

$enrolled_term=end($_POST['enrolled_term']);
Sahil Gulati
  • 15,028
  • 4
  • 24
  • 42
1

Try this : to get last selected value (from array)

$total_selected = count($_POST['enrolled_term']);
$enrolled_term = $_POST['enrolled_term'][$total_selected-1] ; //last index
B. Desai
  • 16,414
  • 5
  • 26
  • 47
1

To get the last element of an array, use array_slice():

$lastElement = array_values(array_slice($_POST['enrolled_term'], -1))[0];

array_slice — Extract a slice of the array

Milan Chheda
  • 8,159
  • 3
  • 20
  • 35
  • sorry one more thing, its working well on localhost, but throwing internal server error on cpanel. and my cpanel not recording any error log. – Ademaintain Jul 30 '17 at 10:53
  • Is it throwing error at this line? because this code isn;t throwing any error or warning. You can check here: https://3v4l.org/5oXGi – Milan Chheda Jul 30 '17 at 10:57
  • Yes i can see its not throwing any error from above, and it works well on localhost too, but when i uload online, it throws internal server error, and when i comment it ouit, site reloads fine. Im not sure maybe my server nt supported. – Ademaintain Jul 30 '17 at 10:59
  • Hmm, this are PHP functions, no additional installations required. May be, check if `$_POST` is not empty, only than should this code execute. – Milan Chheda Jul 30 '17 at 11:04
  • I use $enrolled_term=end($_POST['enrolled_term']); and it worked, i guess array_slice is using higer version of php not supported by my server because it works well on localhost. Thanks so much all the same – Ademaintain Jul 30 '17 at 11:11
  • That's very strange. this functions are not deprecated. – Milan Chheda Jul 30 '17 at 11:16
1

Just get last item in $_POST['enrolled_term'] . Use end() in PHP end($_POST['enrolled_term']) or $_POST['enrolled_term'][count($_POST['enrolled_term']) - 1]

Giang Le
  • 6,446
  • 4
  • 23
  • 26
0

Use count() function to get the last items which equals count-1

$terms = explode(" ", $_POST['enrolled_term']) ;
$enrolled_term =$terms[count($terms)-1];
Osama
  • 2,912
  • 1
  • 12
  • 15