I have 10 elements of Array, how can i remove 5 array
element, with single function.

Answer Posted / vipul dalwala

Thank you, Pankaj for your comments.

Here is the sample code with both array_splice and
array_slice.

<?PHP

$input = array
("a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k");

$output = array_slice($input, 0, 5));

print_r($output); // OUTPUT: Array ( [0] => a [1] => b [2]
=> c [3] => d [4] => e )
print_r($input); // OUTPUT: Array ( [0] => a [1] => b [2]
=> c [3] => d [4] => e [5] => f [6] => g [7] => h [8] => i
[9] => j [10] => k )

$output = array_splice($input, 0, 5));

print_r($output); // OUTPUT: Array ( [0] => a [1] => b [2]
=> c [3] => d [4] => e )
print_r($input); // OUTPUT: Array ( [0] => f [1] => g [2]
=> h [3] => i [4] => j [5] => k )

?>

So again it depends on how you want your input array after
appying the function. As per the question what I understood
is Raheem wants to remove 5 elements from the input array.
Thats why I have suggested array_splice so that after
function my input array has only remaining elements.

Any commets are welcome.

Is This Answer Correct ?    4 Yes 0 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

Explain about PHP cookies?

622


How do functions work?

541


What is use of count() function in php?

571


Suppose we receive a form submitted by a post to subscribe to a newsletter. This form has only one field, an input text field named email. How would we validate whether the field is empty? Print a message "the email cannot be empty" in this case?

682


Is strcmp case sensitive?

521






What is difference between static and constant in php?

502


How to get number of days between two given dates using PHP?

483


How to find length of an array in php ?

558


What is empty php?

497


Why php 7 is faster?

512


What exactly is validating and sanitizing?

621


What is basename php?

519


What is the output of the following php code?

499


Is php developer in demand?

510


Is it possible to destroy a cookie?

540