Remove A Value From A Php Array Based On Its Value

free web hosting
Open Discussion > CONTRIBUTE > Computers > Programming Languages > PHP Programming

Remove A Value From A Php Array Based On Its Value

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

Reply

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

Reply

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

Reply

iGuest
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!

Reply

iGuest
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

Reply

iGuest
Replying to sonesay
A. $key = array_search()
B. Unset($key)

Reply



Got an Opinion! Express your Views! (no registration):-
Add your Reply/ Opinion/ Views/ Comments/ Suggestion/ Questions/ Queries etc.
Posts with decent grammar & English will be accepted and please refrain from profanities.
For asking a Question, We recommend you to sign-up (for free) so that you can track the topic easily.

Nature of your Post*: Opinion/ Reply/ Comments
Question/Query
Feedback to us.
       
Name   Email
Title/Question*

(Maximum characters: 10,000)
You have characters left.

Recent Queries:-
  1. remove value from array php - 1.90 hr back. (1)
  2. php command line value $_get array - 2.17 hr back. (1)
  3. delete value in array with php - 4.81 hr back. (1)
  4. php remove from array - 14.08 hr back. (2)
  5. php remove from array update index - 17.84 hr back. (1)
  6. php remove from an array - 19.09 hr back. (1)
  7. php delete array value - 1.95 hr back. (2)
  8. php delete _value - 23.94 hr back. (1)
  9. remove from php array - 31.98 hr back. (1)
  10. php array remove object - 35.88 hr back. (1)
  11. array delete one value php - 41.03 hr back. (1)
  12. php remove field from array - 41.22 hr back. (1)
  13. php array remove value - 44.19 hr back. (1)
  14. php array remove by id - 46.38 hr back. (1)
Similar Topics

Keywords : remove, php, array, based

  1. Sending E-mails Based On Intervals?
    (7)
  2. Creating Something With Co-ordinates With Php.
    Something like you see in those web-based php games, like a map or som (12)
    Well, i was just wondering if something simple and basic could be made (like a small map where you
    could move around on, like you click up, and your position moves up and reports it to the
    database..etc) with co-ordinates in php . Taking the x-axis and the y-axis into consideration.
    Things like maps which online games have such as web-based space games or legacy-game.net. Thanks
    for any feedback, ~Aldo....
  3. Looking For Command Line Based Sql Modification
    Program Needed For SQL? (1)
    How I use Windows command prompt client and do modifications to my SQL database. If yes, please
    write the commads I should know for that. If no, please get me the software with which I can do it.
    Topic title modified. Proper English is always required when posting in our forum. ....
  4. Php And Irc
    PHP-based IRC web client (5)
    I am wondering whether or not you could create a basic client for IRC using PHP. I know that PHP
    does have IRC functions which I read about on php.net. I am not quite sure whether or not a client
    could be built around this. I do know that there is a CGI IRC client on the web. I use this while I
    am on my Wii I am wanting to incorporate a PHP IRC client into my website and use AJAX to
    automatically reload the chat. Well, if anybody could help that would be great.....
  5. Getting An Array Value Of A Dynamic Variable
    (9)
    Ok i can't believe I need help in php but I can't figure this out... If i have an array,
    lets say called test, how do i get a certain value from that array when referencing it dynamically
    by either a string or another variable? Example: CODE $test=array(1,2,5);
    $name="test"; //Doesn't work: echo $$name[0]; /Doesn't
    work: echo ${$name}[0]; ....
  6. Dynamically Select From 2 Tables Based On Id
    dynamically select from 2 tables based on id (1)
    hi there, im working on a performance request system, and i have three tables that im working with
    right now. the performances table holds the info about the performance (date, times, status, etc.),
    and has two columns, org_type and org_id, which both hold an int value to tell me where the
    organization info is. there are two org types which have respective tables, because we collect
    different information based on the type of org that is registering. is there any way to select info
    from all three tables (dynamically selecting between the two org tables based on org_type,....
  7. Using Multiple Selection Array In Table To Order Data
    Using multiple selection array in table to order data (1)
    have a form that has a multiple select choice, like this: CODE <form method="post"
    action="display.php" <select multiple name="selectsort[]">
    <option value="code">Code</option> <option
    value="amount">Amount</option> <option value="dateammended">Date
    Ammended</option> <option value="expreviewdate">Expiration/Review
    Date</option> <option value="effectivedate">Effective Date</option>
    <option value="e....
  8. Reading Files From Directory To Array, And Using $_get To Get Them
    Simple way to manage lot's of files (2)
    Some user posted a similar problem i had when i tried to figure out how to update content on my
    website in less work as possible. This is just part of the "big" plan i have for my site but it can
    be helpfull to you guys if you like FlatFile. I hope that mod's don't mind me posting the
    same code here and on their forums, couse if they do i'll delete it from their forums
    /biggrin.gif" style="vertical-align:middle" emoid=":D" border="0" alt="biggrin.gif" /> So
    here's the situation. Let's say you have a folder called 'myfilesdirectory' on your
    s....
  9. More Dynamic ?id=browsing With Php (associative Array)
    Just create array and watch php code do the rest (1)
    The thing that has been bugging me for a while was that switch statement that we use to create ID
    browsing (some use If-Ifelse but results are the same for both). I wanted to figure out a way to use
    more dynamic switch statement so that i only need to update my links array in order to create links
    for template. With use of foreach, array_keys, and in_array functions finally i managed to do so.
    Also i'm planing on changing foreach with array_walk but i'll do that later. Now for the
    code.. First we create an associative array something like this CODE $gl....
  10. Remove Unnecessary Decimals With Php?
    (3)
    I have a MySQL DECIMAL field allowing up to 3 decimals to the number. However, many of the numbers
    only have one decimal. An example is 5.2. I want the script to echo ONLY 5.2 and not 5.200. However,
    if the number is 5.211, I want it to echo the whole number, so I can't just use the round()
    function. Is there a way to do this?....
  11. Spliting An Array
    (4)
    I got this excercise from school, im trying to understand it but im unable..can someone help me ?its
    very small problem but im not getting the point. ------------------ Create a string:
    $wholestring = “splitwholestring”; And split it to array using for loop. So that you have a
    array containing all the letters of the string: array == s, array == p, etc. Display results.
    (hint: strlen(), substr() - functions are useful) ------------------....
  12. Converting Characters In A Variable To Individual Values In An Array
    turning variables into arrays (2)
    Say I have a variable such as $nav_item and it had to contents Home . IE: CODE
    $nav_item = 'Home'; How would I make so that $nav_item was an array and
    had the following contents? CODE $nav_item = array ('h', 'o',
    'm', 'e'); With the case changing (ie H would become h and U
    would become u ) EDIT: Okay found out that I could change the case with
    array_change_key_case ($nav_item, CASE_LOWER); ....
  13. Remove Objects From Array With Unset
    (2)
    Have been trying to solve this for a long time now, so I thought I´d give you guys a chance to break
    it! I want to remove an object from an array that I store in a Session object. I´ve written a
    function that takes an array and an id as parameters. Checks if one of the objects has the id I want
    to remove and then it´s supposed to remove it. Quite simple, that´s why I cant see why it doesnt
    work! Here´s the code: CODE function Remove_prod($id2,$AL) {
    print_r($AL); foreach ($AL as $prod) if($prod-....

    1. Looking for remove, php, array, based

*RANDOM STUFF*





*SIMILAR VIDEOS*
Searching Video's for remove, php, array, based

*MORE FROM TRAP17.COM*
advertisement



Remove A Value From A Php Array Based On Its Value



 

 

 

 

ADD REPLY / Got an Opinion! a humble request :-) RAPID SEARCH! Free Hosting [X]
Express your Opinions, Thoughts or Contribute your information that might help someone here.
Ask your Doubts & Queries to get answers.. "Together, We enlight each other!"
Register FREE for AD-FREE forum, Create your own topics, Ask Questions, track topics, setup subscriptions & notifications and Get a Free Website w/ Email and FTP.
500MB Space *No Ads*, CPanel, FTP, PHP, MySQL, EMails - 100% FREE