I need to delete the first 29 values of an array. I searched and there doesn’t seem to be any built in PHP function that allows for this. How can this be done then?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
You can use
array_spliceto remove values from an array:The array is passed as a reference as
array_splicemodifies the array itself. It returns an array of the values that have been removed.Here’s an example for how it works:
In opposite to that,
array_slice(without p) just copies a part of an array without modifying it:Here
array_slice($arr, 29)copies everything from the offset 29 on up to the end while leaving the$arras is.But as you said you want to delete values,
array_spliceseems to be the better choice instead of copying a part and re-assigning the copied part back to the variable like this:Because although this has the same effect (first 29 values are no longer there), you do the copy operation twice: create a new array and copy everything except the first 29 values and then re-assign that value to
$arr(requires copying the whole array again).