Golgappa.net | Golgappa.org | BagIndia.net | BodyIndia.Com | CabIndia.net | CarsBikes.net | CarsBikes.org | CashIndia.net | ConsumerIndia.net | CookingIndia.net | DataIndia.net | DealIndia.net | EmailIndia.net | FirstTablet.com | FirstTourist.com | ForsaleIndia.net | IndiaBody.Com | IndiaCab.net | IndiaCash.net | IndiaModel.net | KidForum.net | OfficeIndia.net | PaysIndia.com | RestaurantIndia.net | RestaurantsIndia.net | SaleForum.net | SellForum.net | SoldIndia.com | StarIndia.net | TomatoCab.com | TomatoCabs.com | TownIndia.com
Interested to Buy Any Domain ? << Click Here >> for more details...


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...

Answer / vipul dalwala

$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...

Answer / 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

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

Answer / yukti vig

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...

Answer / vipul dalwala

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...

Answer / vaneet badhan

$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...

Answer / yukti vig

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...

Answer / pankaj

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...

Answer / phani

$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...

Answer / sunil kumar

<?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...

Answer / vepurnaresh

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

Post New Answer

More PHP Interview Questions

Define about declare construct?

0 Answers  


How to apply Cake php ajax pagination?

1 Answers  


What is meant by public, private, protected, static and final scopes?

0 Answers  


Is PHP runs on different platforms (Windows, Linux, Unix, etc.)?

0 Answers  


How can we destroy the cookie?

4 Answers  


What is T_PAAMAYIM_NEKUDOTAYIM?

0 Answers  


Which function would you use to determine the length of a string in php?

0 Answers  


How does csrf token work?

0 Answers  


Explain what is the difference between mysql_fetch_array() and mysql_fetch_assoc()?

0 Answers  


What is the difference between $message and $$message in php?

0 Answers  


What changes I have to do in php.ini file for file uploading?

0 Answers  


How check variable is set or not in php?

0 Answers  


Categories