Welcome Guest ( Log In | Register)



 
Reply to this topicStart new topic
> Remove A Value From A Php Array Based On Its Value
sonesay
post Feb 10 2008, 11:14 AM
Post #1


|||[ n00b King ]|||
*********

Group: [HOSTED]
Posts: 680
Joined: 20-June 07
From: Auckland
Member No.: 45,102



I have an index array

CODE
$array[0] = 10;
$array[1] = 12;
...


Its kinda late and I'm falling asleep now but I had a quick look through PHP.net, and could not find what I'm after, Maybe I'm too sleepy, sorry, but if anyone knows off the top of their head a way to removed members in that array based on its value, i.e. I wanna remove values 12. I don't really want to do a loop but if I cant find any existing function to do it, then I may have to.

Thanks for any help.
Go to the top of the page
 
+Quote Post
truefusion
post Feb 10 2008, 05:36 PM
Post #2


Ephesians 6:10-17
Group Icon

Group: [MODERATOR]
Posts: 1,893
Joined: 22-June 05
From: The World of Gentoo
Member No.: 8,528
T17 GFX Crew



You may have to make your own function or set up for this. But i did a quicky function that should work fine for your needs:
CODE
function remove_element($arr, $val){
foreach ($arr as $key => $value){
if ($arr[$key] == $val){
unset($arr[$key]);
}
}
return $arr = array_values($arr);
}

You can rename the function if you want.
Go to the top of the page
 
+Quote Post
sonesay
post Feb 10 2008, 08:00 PM
Post #3


|||[ n00b King ]|||
*********

Group: [HOSTED]
Posts: 680
Joined: 20-June 07
From: Auckland
Member No.: 45,102



Thanks again for the help Truefusion. Its too bad PHP does not have a array function for this specific task itself, they seem to have many other functions for other kinds of array modifications.
Go to the top of the page
 
+Quote Post
iGuest
post Mar 16 2008, 02:04 AM
Post #4


Trap Double Mocha Member
***************

Group: Members
Posts: 2,360
Joined: 21-September 07
Member No.: 50,369



thanks
Remove A Value From A Php Array Based On Its Value

Replying to truefusion
This is really good... I needed a script to find the only file in a folder, but it returned '.' and '..' and then the file... Now I just do this:
<?php
$return = delete_value($return, '.');
$return = delete_value($return, '..');
?>
Thanks...


-reply by php-freak!
Go to the top of the page
 
+Quote Post
iGuest
post Mar 19 2008, 09:29 PM
Post #5


Trap Double Mocha Member
***************

Group: Members
Posts: 2,360
Joined: 21-September 07
Member No.: 50,369



strip element from array()
Remove A Value From A Php Array Based On Its Value

Replying to truefusion
Is there away to chop an array depending on the value of a sub-array?

I.E:
$array[0] = 10;
$array[1] = 12;
$allcontent = array("page1", "page2", array("page3a", "page3b"), "page4", "page5");

If my function finds "page3b" I want it to end the parent array at "page2"/
I.E.:
$allcontent = array("page1", "page2");

If this can't be done how about just removing the sub array?
I.E.: $allcontent = array("page1", "page2", "page4", "page5");


-reply by Scoutman57
Go to the top of the page
 
+Quote Post
iGuest
post Jun 10 2008, 04:09 AM
Post #6


Trap Double Mocha Member
***************

Group: Members
Posts: 2,360
Joined: 21-September 07
Member No.: 50,369



Replying to sonesay
A. $key = array_search()
B. Unset($key)
Go to the top of the page
 
+Quote Post

Reply to this topicStart new topic

Collapse

> Similar Topics

Topics Topics
  1. Spliting An Array(4)
  2. Remove Unnecessary Decimals With Php?(3)
  3. More Dynamic ?id=browsing With Php (associative Array)(1)
  4. Reading Files From Directory To Array, And Using $_get To Get Them(2)
  5. Using Multiple Selection Array In Table To Order Data(1)
  6. Dynamically Select From 2 Tables Based On Id(1)
  7. Getting An Array Value Of A Dynamic Variable(9)
  8. Php And Irc(5)
  9. Looking For Command Line Based Sql Modification(1)
  10. Creating Something With Co-ordinates With Php.(12)
  11. Sending E-mails Based On Intervals?(7)


 



- Lo-Fi Version Time is now: 5th September 2008 - 11:11 PM