Deleting A Single Item From The Middle Of An Array?
I have an array that contain objects, I want to be able to test against the id of that object and remove it from the array. In it's simplest for I want to go from this:
$people = array('Tom', 'Dick', 'Harriet', 'Brenda', 'Jo');
...to this...
$people = array('Tom', 'Dick', 'Brenda', 'Jo');
The fact that the items in the array are objects should not matter right? Plus I've already written my loops and conditionals to check which item i need to remove.
function removeItem ( $item ) {
$len = count ( $_SESSION['basket_arr'] ) ;
if ( $len == 1 ) {
emptyBasket ( ) ;
} else {
for ( $i = 0 ; $i < $len ; $i++ ) {
if ( $item->id == $_SESSION['basket_arr'][$i]->id ) {
//$_SESSION['basket_arr'][$i] must go
} } }}
View Complete Forum Thread with Replies
See Related Forum Messages: Follow the Links Below to View Complete Thread
Remove A Single Item From An Array
I have gathered an array of ID's. I need to go throught the array and find a specific ID and remove it from the array. Because we want to keep that ID. Any suggestion of how to do that.
Deleting An Array Item
I have a standard array: $foo = array( 0 => 'abc' 1 => 'def' 2 => 'ghi'); and I have a variable, $bar, which equals 'def' ... and I want to check my array to see if the value of $bar exists in the array, and if it does, delete it from the array. if (in_array($bar, $foo)) { // what goes here??? } I've done this before, but for the life of me, I'm drawing a blank now. Help a bruthah out?
Iterate From Middle Of Array
I want to iterate through an array starting at a known index. However the indexes are not linear. For example I have an array of events keyed by timestamp. $eventList = array (1064263264 => "event1", 10642635555 => "event2", 1064266666 => "event3", 1064267782 => "event4", 1064268812 => "event5"); I basically want to do a "for" or "foreach" but I don't necessarily want to start at key 1064263264 (event1). I'm looking for something Like $startEvent = 10642635555; $endEvent = 1064267782; for ($event = $startEvent; $event < $lastEvent; $event = nextKey()) { print $eventList[$event]; } To make it worse, I can't use a foreach and simply "if" my way out of it because I am already iterating over the entire list and I would like to avoid a O(x^2) algorithm. foreach ($eventList as $currentEvent) { ..... // processing $startEvent = 10642635555; $endEvent = 1064267782; for ($event = $startEvent; $event < $lastEvent; $event = nextKey()) { print $eventList[$event]; } ..... // more processing } Of course there is no "nextKey()" and before rolling my own I thought I would ask if PHP already has a solution for this that I've missed.
Inserting A Value Into The Middle Of An Array: Is This The Best Way?
I'm trying to insert a value into the middle of a simple (numerically-ordered) array, and bump all the later array keys up one. AFAIK, there isn't a function to do this already, so this is the code I came up with... is this the best way of doing it? <?php function array_insert($array, $value, $position) { if (is_array($array)) { $array_out = $array; // so I don't mangle it during foreach foreach ($array as $key => $val) { if ($key < $val) { $array_out[$key] = $val; } else { $array_out[$key+1] = $val; } } $array_out[$position] = $value; return $array_out; } return false; } ?>
Insert Into Middle Of Array
I've read over all of the array functions at php.net. The closest thing to what I'm looking for is array_push(). But I can't figure out how to use push to put data in at a specific point in the array, just the beginning. I want to insert data in the middle of my arrays while preserving/pushing back all the values behind it: array(0,1,2,3,4); insert_into_array at 2(value1,value2,value3) so then my array is 0,1,2,value1,value2,value3,3,4 How do you do this? I'm feeling befuddled, maybe I should get more sleep (I really don't get enough sleep:()
How To Add An Element To The Middle Of An Associative Array ?
I got an array that consists of elements that are arrays also. Now I wish I could add an element to the middle of it. Let mi give you an example: array ( - array(1,15,apple), - array(2,28,banana), - array(3,41,orange) } I would like to add, let's say, carrot on 2nd position: array ( - array(1,15,apple), - array(2,57,carrot), - array(3,28,banana), - array(4,41,orange) } Array_splice() does not work (or maybe i use it in a wrong way? - it splits an adding element for, in that case, 3 elements). Array_push() adds only to the end of an array. can samobody tell me how to add and element to the middle of an associative array?
Random Item Of An Array
What is the best way of picking out a random item of an array, regarding speed and CPU-usage ? I need a super fast way of picking out an ID from my MySQL db.
Shopping Cart Item Array
Trying to build a small simple shopping cart and I am having difficulty getting the solution. The cart is displaying each product, but it is overwriting the values. So if I have added 10 products, it shows ten items, but each item has the values of the last product I added. I think that this may be due to me trying to use a key to store both an array and string at once, but Iam not sure. I have looked at other examples but due to different methods, syntax is throwing me off. Code:
Arrays : Returning A Reference To An Array Item ??
I am currently storing a set of objects inside an array, $itemlist = array(); $itemlist[] = new item("myitem"); //... and I am looking to develop a search function, which returns a reference to the found item. function &search_item($itemlist,"myitem") { //... } I am trying to figure out how to obtain a reference to a given item in an array. Indeed, foreach() and each() seem to work on copies of the data in the array. For example: $array = array("a","b","c","d"); print_r($array); foreach($array as $v) $v="xxx"; print_r($array); // $array is unchanged reset($array) ; while(list(,$v)=each($array)) $v="xxx"; print_r($array); // $array is unchanged, again
Removing Item From Array Then Overwriting File
I have a bookings.txt file which has a list of dates each on a new line, for example: - 21/06/07 22/06/07 29/06/07 25/12/07 01/01/08 I also have a variable $event_dd_mm_yy which I want to remove from this file if found. Code:
Single Array To Multidimensional
I have a string, comma separated, with links and their respective URLs in it. Example: Google,http://www.google.com,Yahoo!,http://www.yahoo.com,WikiPedia,http://www.wikipedia.org etc, etc. i make an array of this with explode( ',', $string ); now what i wonder is how can i turn this array into something like this: $array[0]['name'] = Google $array[0]['url'] = http://www.google.com $array[1]['name'] = Yahoo! $array[1]['url'] = http://www.yahoo.com $array[2]['name'] = WikiPedia $array[2]['url'] = http://www.wikipedia.org I hope/think this is all clear ...
Removing A Single Value From $_SESSION Array.
What I have here is three select boxes one for all the tables in the db, one listing the table columns in that table and a third to collect the table columns selected stored in a session called $_SESSION['collections']. Up to this point all that works beautiful the session stores the selected collections. But heres what I want to be able to do: I want to remove single values from the $_SESSION['collections']. How do I do that? Code:
Select Single Column From Mysql Into Array
Is there a way to select a single column from a mysql database and directly put the results into an arrray? Here is the workaround below, but I would like to elimniate the array_push step: $uids=array(); $res = mysql_db_query("db", 'select uid from tbl;', $link); while ($row = mysql_fetch_row($res)) { array_push ($uids, $row[0]); }
Mailparse_msg_get_structure Returning Single Valued Array
I am writing code to strip out and write key data to a database from incoming e-mail. I am using the mailparse functions to handle the incoming mail. My code pulls in the e-mail fine, but the array returned by the mailparse_msg_get_structure() only has one row. The entire contents of the /var/mail/$user file is being put into one row instead of it being broken apart into individual messages. Here is the code:
Deleting An Element In An Array??
Here is the problem: I've got an array with the following elements $array = ("ice","ice","polka","skate","polka"); thats 2 polka, 2 ice and 1 skate Now i want someway of removing just one of the polka's from it.. so that i'd be left with: $array = ("ice","ice","skate","polka"); what i did was a basic search with for loop and break Code: for($x=0;$x<sizeof($array);$x++) { if($array[$x] == "polka") { echo("match ".$x); break; } } alls well n good till now.. now i've got the index value(2 in this case) of the element which needs to be deleted.. but what should be done now?? is there any function which'll let me delete a particular element in an array? Is there any other way to go around this?
Deleting Array Elements
I need to delete a single element from a one dimensional array. something like. somefunctionname $arrayname[$key]; does anyone know what somefunctionname is or how this is done?
Deleting An Element From An Array
is there some handy and short method to delete an element from an array and than shifting the rest of elements one place ahead e.g array(1,2,3,4,5,6) becomes array(1,2,4,5,6)
Deleting Multidimensional Array
if i have a multi dimentional array, eg: $arr[x][y] = ''; and i want to delete the enrite thing, does unset($arr); rowk, or so i have to delet the second arrays, or how do i do it???
Deleting Blank Entries From Array?
The resulting arrays that i get from my select statements always seem to have some blank entries, in addition to the correct ones. It is rather odd, is there any way i can scan through it and delete these blank entries? Here is the print_r of the arrays. PHP Code:
Clearing/deleting $_SESSION Array?
I have a $_SESSION array in a nested loop that I need to clear for multiple passes and cannot seem to figure out how to this. I have read the manual on unset and such but there seems to be no clear technique that I can ascertain. PHP Code:
Checking If A Value Is Not In An Array And Deleting Record From Database
I have been trying to figure this one out and have come up against a brick wall although I know it should be relatively simple. I have a form that passes an array to a php script from a multiple select box that is created dynamically. If a user deselects an item then I want to delete the record from the database but am having a big problem with it. The table in the dbase is quite simple with just three fields: id classid formid Basically, the form send through a classid and a formid value and I want to delete any records that have a classid that are not in the form array and have a formid which is passed as a hidden value. I suppose really what I want is to know how to see if a classid is not in the array that is passed.
Deleting Database Rows From An Array Of Variables
I have a form with a list of checkboxes, which are each filled with a date in the format 27012007, for example. The name of each of the checkboxes is the same as the date. Basically when the user presses the submit button I would like all rows in the database table "users" with the corresponding column "date" to be deleted when the form variables date correspond to those in the "date" rows. I think all the checked dates might also need to be put into an array. Would anyone know how to do this?
Batch File Deleting And Folder Deleting
I have a folder in my web site where I used php to create and copy in files. but now I can't delete them easily. The only way I know how that is possible is through php. I get an error the following ftp error: 550 th: Permission denied I had the chmod() set to 0777 on every file and folder. But I still seem to loss control over the files when I try and do things with them through other means. I think it is because the user I use to access the files via ftp a diffrent user. So I tryed to add a function with chown() for the new files I was posting and uploading. It didn't work on those new files. so it looks like the only thing I can do is use the unlink() function which deletes the files fine. The only problem is now I have something like 100 sub directories all with files in them. I need to figure out a way to batch delete them. I can't find a script that goes through and finds all file contents including all sub directories and unlink those things. I found some that will use a for loop to list out the contents of a folder but it is just files. No sub directories. Any body got any ideas on which functions I should use or know where a script is that I can base mine off of. I am not sure how to approach this since what I tryed with chown and chmod has failed thus far. I currently have no code and am starting from scratch on this batch flushing of my directory.
Middle Bit Of A String
I want to extract two bits from a string, this is an example of the string: "varible1=IWantThis&varible2=IWantThis&junkvarible=junk" I want to select where it says 'IWantThis' the first occurance is a set distance from the left, and is not a set length. it ends at the &. the second starts from the second '=' till the second '&' and this value isn't a set width. The varibles you can see in that are not a set width, the varible names are. It is in a string so with that and various end positions I don't know how to get the code I want.
Getting Text From The Middle Of A String
I have a text file that I'm going to load into a variable. What I want to do is create a second varibale that contains the comments from the html in the text file so any time it finds <!-- it will put the text afterwards into the comment variable untill it reaches --> and then it will stop.
Write To Middle Of File
i am creating a system that allows the user to input information that is sent to a HTML file as a new line on a table, atm i am just making the php file add <tr><td>*info*</tr></td> and it works, but now i need the table to be complied the other way around with the newest information at the top so this basically needs me to have the header HTML with the table start, the newest entry then the older ones. I realize this means i will need to sepeare the file into 2 and put the new info in the middle somewhere so i was wondering the best method to do this?
Write In The Middle Of A File Without Overwriting
How can I use fwrite() and fseek() in order to write data in the middle (or anywhere else) of a file without overwriting existing data ? People told me that I should load the file into memory (a variable) and use concatenation. But, according to me, It isn't clean to load an entire file into memory... Imagine a huge file of 5 GB !!!
Load Page In Middle Without Anchors
i am using php and have an "update.php" page that is a long list of input forms for an admin user to update database entries. each entry has its own "update" button. so when the user edits an entry and clicks "update" s/he is directed to a processing script which updates the database and returns the user to "update.php". heres my problem: the user is returned to the top of page (which is fairly long) so they have to scroll back down every time if they want to edit a number of subsequent entries. i would like the "update" submission to somehow save the current page view so that the user is returned to precisely the same scrolled down view as before. can this be done without anchors? i know i could use anchors but am i right that this would cause some scrolling up or down to the nearest anchor in general? i would prefer some way of doing it by percentage, pixels, etc so as to cause a sort of transparent departure and return to the page.
HOWTO: Output A Picture In The Middle Of A Page
I want to generate a web page using PHP, and in the middle generate a graphic I read from another server. So the graphic is in $picture. So I can either write $picture to a temp file and then use an ordinary <img src=filename> to get the browser to display it, or output it by itself in a new page, because then I have a chance to output the correct header so I don't just get garbage on the screen. Evidently first approach is poor because I have to write a local file, and second approach is not what I want anyway. Is there a way to do this? That is, I have $picture that contains an image, and I want to output it here and now in this HTML page I am in the middle of creating, and have it show as a picture.
Regex - Middle Untouched But Replace The Outer Parts
just learning regex is it posible ? say i have a string " this is a really funny string" and want to leave the middle untouched but replace the outer parts EG ens up with "that was a really funny rope" so for example find a string with "this is" on the begining and replace it with "that was" and with "string" on the end and replace it with "rope" BUT whatever is in beteween remains untouched.
Iterator First Item?
Let's say I have the code: foreach($data as $row) { do something with each $row } Is there a way to do something specific to the first item iterated? Pseudocode: foreach($data as $row) { if first time through iteration do something with $row else do something which each remaining $row } Is this possible? I think I could use a regular 'for' loop, using the length of the $data array as a guide, and then an 'if..else' structure to see if it is the first time through the 'for' loop, but I was looking for a more elegant solution.
Trying To Split The Item...
I'm trying to make a search thru my mySQL database, so it any keywords are found then I'm adding the details to the next item in an array that I create. I've tested the array and it is storing the information into the array properly because it echoes out all of the information correctly. My problem is that, at the beginning of each item in the array I have information that tells me what part of the database it was pulled from and the id number and also how many times the a keyword was found in that specific database entry. So you have something like this.. Code:
Get Last Item In URL String
I realize how elementary this is, I apologize. Maybe that's why I can't find the right answer. I've tried everything from EXPLODEing the string to SUBSTR_REPLACE. Can anyone tell me how to just get the last item in a URL string? Like turn this: /mydirectory/myimg.jpg into this: myimg.jpg
Search Item In Database
I made a function that does a case insensitive search. This works fine. But i'd like to improve that script so that it also returns items from the database that are partly similar to the search string. this is the script atm:
Delete Item From Database
I still can't figure out how to delete an item from my shoppingcart. I have tried different ways to do this, but its impossible. -a button or link that deletes a item from my database -the php script that deletes it here is a suggestion: $slett = "DELETE ID, VARE, MODELL, STR, ANTALL, PRIS, TOTALPRIS FROM USER_TRACK WHERE USER_ID = "$user_id""; But how can i assign this to a button?
Updating Item Numbers (or IDs)
After a while of deleting records in a MySQL db, there gets to be the gaps in the id numbering system. i.e. 1, 2, 3, 6, 7, 12, and so on. Is there a way to renumber the id system in a table for 1, 2, 3, 4, 5, 6, etc without manually going in and changing those numbers?
Returning Autogenerated ID On New Item
Lets say you have a form that when saved to the mysql database creates a unique incremental id. The form is just the first part of a series of things the user can do, so once the ID is created, the page would need to know immediately what this ID is. Is there a way to grab this ID instantly once it is created? Obviously if this were a sign-up for and a new user name and password are involved, you can have the user login after the initial set up and this select statement would bring back the ID. But the situation I have doesn't work like that. The way I have been dealing with this is simply to have a select statement after the initial insert that requests the record based on a couple of other fields that are likely unique. But this seems like a rather kludgy way of doing it. Is there some other solution.
|