How can I remove a element from an array in PHP

Suppose I have an array in PHP as shown below
a[0] = "0";
a[1] = "1";
a[2] = "2";

How can I remove the first element in an array and then all the rest of the elements will shift their position to the left.
i.e. a[0] = "1";
a[1] = "2";

Thanks!
mawinghoAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

afzzCommented:
see the following function

http://in2.php.net/array_shift
0
mawinghoAuthor Commented:
How about the element not the first one?
0
Guy Hengel [angelIII / a3]Billing EngineerCommented:
this should do
unset(a[0]);

Open in new window

0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

afzzCommented:

$a = array("0","1","2");
$firstelement = array_shift($a);
print_r($a);
//Now array is $a[0]="1", $a[1] ="2"

Open in new window

0
afzzCommented:
0
mawinghoAuthor Commented:
unset is doesn't work
0
afzzCommented:
array_splice($a, 0); //will remove the first element
array_splice($a, 1);//Will remove the second and so on..
0
mawinghoAuthor Commented:
I want to remove just ONE element
0
mawinghoAuthor Commented:
any function can help me to remove JUST any ONE element from an array.
0
hernst42Commented:
array_hist does excatly that what you posted in your example. If it's not working please give a new, correct example.
0
mawinghoAuthor Commented:
is this a valid function =.=''
0
afzzCommented:
function remove_element($array,$element){
array_splice($array,$element);
return $array;
}
//This funtion will remove the element you want from your array
$a = remove_element($a,0);
0
mawinghoAuthor Commented:
If I have an array

a, b, c, d, e, f, g

now I remove c
the function SHOULD give me back

a,b,d,e,f,g
0
hernst42Commented:
ok try this:



function removeElement($arr, $element) {
   unset($arr[array_search($element, $arr)]);
   return array_values($arr);
}
 
$a = array('a', 'b', 'c', 'd', 'e', 'f', 'g');
print_r(removeElement($a, 'c'));

Open in new window

0
Ali KayahanFull Stack DeveloperCommented:
Hi mawingho: array_splice() function is what you need.. its usage is ;
array_splice(Your array,starting index,count of elements that you want to remove) ;
Your array = $a ;
starting index = 1 ;(Will start remove from index 1)
count of elements = 1 (to remove 1 element) ;
    Also array_shift(Your Array) will remove the first element in your array.
After applied all,to shift array one left you may use sort() ;
sort(your array) ;
0
afzzCommented:

function remove_element($a, $element) {
return array_splice($a,array_search($element, $a));
}
$a = array('a', 'b', 'c', 'd', 'e', 'f', 'g');
print_r(remove_element($a, 'c'));

Open in new window

0
hernst42Commented:
afzz that code does not work, its missing a,b but c and the rest is still in there, haven't you tried it?
0
afzzCommented:
ok try this it should work
function remove_element($a, $element) {
return array_splice($a,array_search($element, $a),1);
}
$a = array('a', 'b', 'c', 'd', 'e', 'f', 'g');
print_r(remove_element($a, 'c'));

Open in new window

0
hernst42Commented:
@afzz
that does not work neither result is only c. Is it so hard to test the output of the code before posting worng solutuions, so it works as the asker expects.
0
afzzCommented:
sorry I did not get your question right. Try the following I have checked it works.
<?
function remove_element($a, $element) {
$tmp = array_splice($a,array_search($element, $a),1);
sort($a);
return $a;
}
$a = array('a', 'b', 'c', 'd', 'e', 'f', 'g');
print_r(remove_element($a, 'c'));
?>

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Web Languages and Standards

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.