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

Tell me what are the encryption techniques in php?

572


Why is overriding runtime?

654


Is time a dependent variable?

616


When to use get and post request?

629


How is it possible to propagate a session id?

579






Explain me is multiple inheritance supported in php?

614


What is the use of return in php?

565


What is the purpose of the '.myi' file extension? What do thes file contain?

591


can you give me an example code of calling java script function in php variable using AJAX.or with out ajax??????

1414


How do you clear environment variables?

621


What are magic constants in php?

665


What is lazy loading in php?

586


What is warning – “cannot modify header information – headers already sent”?

718


How can you send http header to the client in php?

579


What is meant by pear in php? What is the purpose of it?

596