Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

How can I remove a element from an array in PHP

Posted on 2008-06-13
20
Medium Priority
?
213 Views
Last Modified: 2013-11-19
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!
0
Comment
Question by:mawingho
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 8
  • 6
  • 4
  • +2
20 Comments
 
LVL 4

Expert Comment

by:afzz
ID: 21776604
see the following function

http://in2.php.net/array_shift
0
 

Author Comment

by:mawingho
ID: 21776610
How about the element not the first one?
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 21776616
this should do
unset(a[0]);

Open in new window

0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 4

Expert Comment

by:afzz
ID: 21776617

$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
 
LVL 4

Expert Comment

by:afzz
ID: 21776629
0
 

Author Comment

by:mawingho
ID: 21776631
unset is doesn't work
0
 
LVL 4

Expert Comment

by:afzz
ID: 21776667
array_splice($a, 0); //will remove the first element
array_splice($a, 1);//Will remove the second and so on..
0
 

Author Comment

by:mawingho
ID: 21776674
I want to remove just ONE element
0
 

Author Comment

by:mawingho
ID: 21776677
any function can help me to remove JUST any ONE element from an array.
0
 
LVL 48

Expert Comment

by:hernst42
ID: 21776693
array_hist does excatly that what you posted in your example. If it's not working please give a new, correct example.
0
 

Author Comment

by:mawingho
ID: 21776701
is this a valid function =.=''
0
 
LVL 4

Expert Comment

by:afzz
ID: 21776729
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
 

Author Comment

by:mawingho
ID: 21776749
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
 
LVL 48

Expert Comment

by:hernst42
ID: 21776769
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
 
LVL 14

Expert Comment

by:Ali Kayahan
ID: 21776786
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
 
LVL 4

Expert Comment

by:afzz
ID: 21777132

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
 
LVL 48

Expert Comment

by:hernst42
ID: 21777153
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
 
LVL 4

Expert Comment

by:afzz
ID: 21777185
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
 
LVL 48

Expert Comment

by:hernst42
ID: 21777207
@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
 
LVL 4

Accepted Solution

by:
afzz earned 2000 total points
ID: 21777238
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

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
Q&A with Course Creator, Mark Lassoff, on the importance of HTML5 in the career of a modern-day developer.
In this tutorial viewers will learn how to style elements, such a divs, with a "drop shadow" effect using the CSS box-shadow property Start with a normal styled element, such as a div.: In the element's style, type the box shadow property: "box-shad…
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…
Suggested Courses

609 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question