Follow Our FB Page << CircleMedia.in >> for Daily Laughter. We Post Funny, Viral, Comedy Videos, Memes, Vines...

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

Answers were Sorted based on User's Feedback

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

\$a = array('a','b','c','d','f','g','h','i','j','k');

array_splice(\$a, 0, 5);

 Is This Answer Correct ? 12 Yes 2 No

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

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 (  => a  => b 
=> c  => d  => e )
print_r(\$input); // OUTPUT: Array (  => a  => b 
=> c  => d  => e  => f  => g  => h  => i
 => j  => k )

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

print_r(\$output); // OUTPUT: Array (  => a  => b 
=> c  => d  => e )
print_r(\$input); // OUTPUT: Array (  => f  => g 
=> h  => i  => j  => 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

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

thats should be array_slice

array_splice is used remove a portion of the array and
replace it with something else

 Is This Answer Correct ? 2 Yes 0 No

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

Hi Yukti,

You can also use array_slice but array_splice is also not
the wronng one.

array_splice function can take four arguments if you omit
the fourth argument which is for replacement array this
function will serve the same purpose as array_slice.

 Is This Answer Correct ? 1 Yes 0 No

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

\$a=array(2,4,1,3,22,15);

for(\$v=0;\$v<=4;\$v++)
{
unset(\$a[\$v]);
}

This code will remove elements "2,4,1,3,22" from array \$a
and the output will be : "15"

 Is This Answer Correct ? 2 Yes 1 No

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

Agreed Sir , No arguments .. :))

 Is This Answer Correct ? 0 Yes 0 No

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

NO Dear vipul Both have great diff. array_slice and
array_splice first one remove from start and return
remaining while second one will have value in array not
remaining.

 Is This Answer Correct ? 0 Yes 0 No

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

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

print_r(\$output);

 Is This Answer Correct ? 0 Yes 0 No

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

<?php
\$a=array(1,2,3,4,75,46,17,8,9,10);
\$size=count(\$a);
\$position=4; //whose number is 75
for(\$i=\$position;\$i<\$size-1;\$i++)
\$a[\$i]=\$a[\$i+1]; //5th element is deleted
for(\$i=0;\$i<\$size-1;\$i++) //displaying the array
echo \$a[\$i]."<br>";
?>

for any doubt contact sunilnagpal30@yahoo.in

 Is This Answer Correct ? 0 Yes 0 No

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

Hi guys...this is one of the answer i found and tested in my
dreamweaver make use of it....

<?php

\$a = array('a','b','c','d','f','g','h','i','j','k');

array_splice(\$a, 0, 5);

for(\$b=0;\$b<=count(\$a);\$b++)
{
echo \$a[\$b]."<br>";
}
?>

 Is This Answer Correct ? 0 Yes 0 No

More PHP Interview Questions

Suppose your Zend engine supports the mode <? ?> Then how can u configure your PHP Zend engine to support <?PHP ?> mode ?

Suppose the variable \$var1 is set to 10 and the \$var2 is set to the character var1, what's the value of \$\$var2?

For image work which library is used in php?

How does php work with apache?

What is a static variable in php?

How is session data stored?

What is the use of limit in mysql?

What is the difference between \$message and \$\$message ?

How can I check if a value is already in an array?

Can you give example for trait in php?

How do I expire a php session after 30 minutes?

What is the difference between mysqli_fetch_object() and mysqli_fetch_array()?

Categories
• PHP (3029)
• Laravel PHP Framework (1078)
• WordPress (1257)
• Drupal (621)
• Joomla (294)
• Magento PHP (281)
• CodeIgniter (272)
• Zend (235)
• CakePHP (128)
• Yii PHP Framework (148)