PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
I am trying to remove an element from an array by using array_splice. If this is wrong then please tell me.
I have re-read the php manual several times but still this doesn't work
$key_id = array_search($student_id, $student_list);
if (!$key_id) {
echo "There was an error. Student not found in student list.";
}
echo "key_id is: ".$key_id;
$student_list = array_splice($student_list, (1+$key_id), 1);
I have tried one other variation where I just use $key_id instead of (1+$key_id)
any ideas?
$key_id = array_search($student_id, $student_list);
if (!$key_id)
echo "There was an error. Student not found in student list.";
else
{
echo "key_id is: ".$key_id;
array_splice($student_list, $key_id, 1);
}
//print('<pre>'); print_r($student_list); print('</pre>');
array_splice alters the array given as parameter and returns the elements it removed.
p.s.: using unset array_search and foreach will be still able to iterate the array