Re: [PHP] Simple array question, array delete
On 4/7/2005 5:19:24 AM, [EMAIL PROTECTED] wrote: > On Wed, April 6, 2005 6:09 am, Ryan A said: > > > when the user gives me a number, i have to check if its in the array > and > > > delete that entry...how do i do that? > > > I have looked at the manual but have gotten confused with array > > > pop,splice,array_key_exists etc > > > > http://php.net/array_search > > http://php.net/unset > > > > It can be confusing at first, with all the different functions that all > > seem to do not quite what you want. :-) > Exactly...theres a s**tload of array functions there...and php being so robust I figured there would be one simple function instead of combining two I have unsed unset in the past...but not array_search Thanks, Ryan -- No virus found in this outgoing message. Checked by AVG Anti-Virus. Version: 7.0.308 / Virus Database: 266.9.4 - Release Date: 4/6/2005 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Simple array question, array delete
On Wed, April 6, 2005 6:09 am, Ryan A said: > when the user gives me a number, i have to check if its in the array and > delete that entry...how do i do that? > I have looked at the manual but have gotten confused with array > pop,splice,array_key_exists etc http://php.net/array_search http://php.net/unset It can be confusing at first, with all the different functions that all seem to do not quite what you want. :-) -- Like Music? http://l-i-e.com/artists.htm -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] Simple array question, part 2
Nope, array_shift()... is taking off the last one too Josip sent me the solution: $array = array_values($array); Cheers, Ryan On 4/6/2005 6:45:40 PM, Jay Blanchard ([EMAIL PROTECTED]) wrote: > [snip] > > Nope, array_pop is just deleting the last key/valuei need to reindex > > it > > without deleteing anything. > > [/snip] > > > > Always reply to the list ('reply all') as the individual who answered > > you might not be there. Always. I mean it. > > > > You > shouldn't just delete an item from an array, it is improper > handling > Thats right, it's > not array_pop(), its array_shift()... > musta' had a > brain fart and you didn't see the other array functions -- No virus found in this outgoing message. Checked by AVG Anti-Virus. Version: 7.0.308 / Virus Database: 266.9.3 - Release Date: 4/5/2005 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Simple array question, part 2
Just so you know what is happening, the numbers are keys (index) for each array element, not the order number. It's the same as if you had named the elements themselves. Like this: ['zero']=>158 ['one']=>169926 ['two']=>169931 ... Or [0]=>158 [12]=>169926 [5]=>169931 ... Deleting an element won't reorder the names of the elements since php wouldn't know how. On Apr 6, 2005, at 12:37 PM, Ryan A wrote: Hey, I have a $data array like this: [0] => 158 [1] => 169926 [2] => 169931 [3] => 169932 [4] => 169933 then when i delete the first one ([0] => 158) it becomes like this: [1] => 169926 [2] => 169931 [3] => 169932 [4] => 169933 how do I get it to sort again from 0,1,2,3 etc? I tried sort(), ksort(),reset() etcbut it still shows as starting from 1 instead of resorting the keys starting from 0 what function should i lookup/use? Thanks, Ryan -- No virus found in this outgoing message. Checked by AVG Anti-Virus. Version: 7.0.308 / Virus Database: 266.9.3 - Release Date: 4/5/2005 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- Brent Baisley Systems Architect Landover Associates, Inc. Search & Advisory Services for Advanced Technology Environments p: 212.759.6400/800.759.0577 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Simple array question, part 2
PERFECT! Thanks mate. -Ryan On 8/6/2005 6:47:22 PM, Josip Dzolonga ([EMAIL PROTECTED]) wrote: > Ryan A wrote: > > > > >what function should i lookup/use? > > > > > >Thanks, > > >Ryan > > > > > This will do the job : > > $array = array_values($array); > > > > Hope this helps, > > > > -- > > Josip Dzolonga > > http://josip.dotgeek.org > > > > jdzolonga[at]gmail.com > > > > -- > > PHP General Mailing List (http://www.php.net/) > > To unsubscribe, visit: http://www.php.net/unsub.php -- No virus found in this outgoing message. Checked by AVG Anti-Virus. Version: 7.0.308 / Virus Database: 266.9.3 - Release Date: 4/5/2005 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] Simple array question, part 2
[snip] Nope, array_pop is just deleting the last key/valuei need to reindex it without deleteing anything. [/snip] Always reply to the list ('reply all') as the individual who answered you might not be there. Always. I mean it. You shouldn't just delete an item from an array, it is improper handling Thats right, it's not array_pop(), its array_shift()...musta' had a brain fart and you didn't see the other array functions -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Simple array question, part 2
Hey, I have a $data array like this: [0] => 158 [1] => 169926 [2] => 169931 [3] => 169932 [4] => 169933 then when i delete the first one ([0] => 158) it becomes like this: [1] => 169926 [2] => 169931 [3] => 169932 [4] => 169933 how do I get it to sort again from 0,1,2,3 etc? I tried sort(), ksort(),reset() etcbut it still shows as starting from 1 instead of resorting the keys starting from 0 what function should i lookup/use? If you're always taking the first element, why not use array_shift? http://us2.php.net/manual/en/function.array-shift.php array_shift() shifts the first value of the array off and returns it, shortening the array by one element and moving everything down. All numerical array keys will be modified to start counting from zero while literal keys won't be touched. If array is empty (or is not an array), NULL will be returned. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Simple array question, part 2
Ryan A wrote: what function should i lookup/use? Thanks, Ryan This will do the job : $array = array_values($array); Hope this helps, -- Josip Dzolonga http://josip.dotgeek.org jdzolonga[at]gmail.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] Simple array question, part 2
[snip] I have a $data array like this: [0] => 158 [1] => 169926 [2] => 169931 [3] => 169932 [4] => 169933 then when i delete the first one ([0] => 158) it becomes like this: [1] => 169926 [2] => 169931 [3] => 169932 [4] => 169933 how do I get it to sort again from 0,1,2,3 etc? I tried sort(), ksort(),reset() etcbut it still shows as starting from 1 instead of resorting the keys starting from 0 what function should i lookup/use? [/snip] http://www.php.net/array_pop -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Simple array question, array delete
On Apr 6, 2005 3:27 PM, Ken <[EMAIL PROTECTED]> wrote: > > On Apr 6, 2005 3:09 PM, Ryan A <[EMAIL PROTECTED]> wrote: > > > Hey, > > I have a $data_recs array like this: > > > > 12 > > 445 > > 45655 > > 4 > > 343 > > etc > > > > when the user gives me a number, i have to check if its in the array and > > delete that entry...how do i do that? > > I have looked at the manual but have gotten confused with array > > pop,splice,array_key_exists etc > > > > Thanks, > > Ryan > > > > -- > > No virus found in this outgoing message. > > Checked by AVG Anti-Virus. > > Version: 7.0.308 / Virus Database: 266.9.3 - Release Date: 4/5/2005 > > > > -- > > PHP General Mailing List (http://www.php.net/) > > To unsubscribe, visit: http://www.php.net/unsub.php > > > > foreach ($array as $key=>$value) > { > if(strpos($value, $userValue) > { > unset($array['$key']); > } > } > > i believe... not sure if it works or not > > hope this helps > > > sorry the if bit is not strpos if ($value == $userValue) { // the unset part here }
Re: [PHP] Simple array question, array delete
On Apr 6, 2005 3:09 PM, Ryan A <[EMAIL PROTECTED]> wrote: > > Hey, > I have a $data_recs array like this: > > 12 > 445 > 45655 > 4 > 343 > etc > > when the user gives me a number, i have to check if its in the array and > delete that entry...how do i do that? > I have looked at the manual but have gotten confused with array > pop,splice,array_key_exists etc > > Thanks, > Ryan > > -- > No virus found in this outgoing message. > Checked by AVG Anti-Virus. > Version: 7.0.308 / Virus Database: 266.9.3 - Release Date: 4/5/2005 > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, visit: http://www.php.net/unsub.php foreach ($array as $key=>$value) { if(strpos($value, $userValue) { unset($array['$key']); } } i believe... not sure if it works or not hope this helps
Re: [PHP] Simple array question, array delete
Ryan A wrote: Hey, I have a $data_recs array like this: 12 445 45655 4 343 etc when the user gives me a number, i have to check if its in the array and delete that entry...how do i do that? I have looked at the manual but have gotten confused with array pop,splice,array_key_exists etc Thanks, Ryan The code follows : if (in_array($number, $data_recs)) { $key = array_search($number, $data_recs); // Get the key unset($data_recs[$key]); // Unset the variable } -- Josip Dzolonga http://josip.dotgeek.org jdzolonga[at]gmail.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Simple array question
On Thu, 23 Oct 2003 14:01:59 -0500, you wrote: >Is there a simple way to return the key (name) of one element in an >array? I looked up key() in the docs, but there are no examples or >notes... Ok, to make sure I understand you: given an array ('apple' => 'red', 'banana' => 'yellow') you want a function where you submit 'red' and the function returns 'apple'? The problem is that keys have to be unique, but values don't. What should the function return for this array? ('apple' => 'red', 'banana' => 'yellow', 'tomato' => 'red') I can see two choices - if values are guaranteed unique, or if you only care about the last hit, use array_flip() to transform the array, then do a normal lookup: $a = array_flip ($a); $key = $a['red']; If you want /all/ the keys, just iterate over the array: $result = array(); $target = 'apple'; foreach ($a as $key => $val) { if ($target == $val) { $result[] = $key; } } -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Simple array question
On Friday 24 October 2003 03:01, René Fournier wrote: > Is there a simple way to return the key (name) of one element in an > array? I looked up key() in the docs, but there are no examples or > notes... array_search() -- Jason Wong -> Gremlins Associates -> www.gremlins.biz Open Source Software Systems Integrators * Web Design & Hosting * Internet & Intranet Applications Development * -- Search the list archives before you post http://marc.theaimsgroup.com/?l=php-general -- /* Neuroses are red, Melancholia's blue. I'm schizophrenic, What are you? */ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Simple Array question
I think what you want is: Then on your next page, you can just do: foreach($_POST['id'] AS $row) echo $row; Or whatever, and that way you only get what you need. On Tue, 2003-07-29 at 13:08, Ryan A wrote: > Hi, > If i am posting something like this from a form: > > > > How do i get the value of id[]? > eg: > I dont want the "sh1" and "sh2", i just want the "3" and "4" > > The reason i dont wan the sh1 and sh2 is those are dynamic and will be > changing, I just need the values (in this case 3 and 4) to do some > comparasion and assignment operations > > Kindly reply. > > Thank, > -Ryan -- Adam Voigt ([EMAIL PROTECTED]) Linux/Unix Network Administrator The Cryptocomm Group -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] simple array question
Jose, Try this: for ($i = 0; $i < $whatever; $i++) { $myArray[] = // ... } /bsh/ Jose Jeria wrote: > in javascript i can build an array doing like this: > > for(i=0; i < whatEver.length; i++){ > myArray[myArray.length] = // > } > > What is the equivalent to this in PHP? > Is this the only way: > $myArray[count($myArray)] = //.. > > /J > > > > -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] simple array question (Am a Newbie kindly excuse.)
>Which gives out the output: >Tesing arrays: >ryan1, >ryan1.2, >ryan2, >ryan2.1.1.1.1.1, >ryan3, >ryan3.2, > >my question/problem is simply this: how do i stop the last comma from >coming? Hi, See http://www.php.net/manual/en/function.implode.php Pekka http://photography-on-the.net/ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] simple array question (Am a Newbie kindly excuse.)
echo join (',', $a1); On Fri, 10 May 2002, r wrote: > Here is the code: > $a1[]="ryan1"; > $a1[]="ryan1.2"; > $a1[]="ryan2"; > $a1[]="ryan2.1.1.1.1.1"; > $a1[]="ryan3"; > $a1[]="ryan3.2"; > reset($a1); > > print("Tesing arrays:"); > while(list($aa1,$aa2) = each ($a1)) > { > print($aa2 . ","); > } > > ?> > > Which gives out the output: > Tesing arrays: > ryan1, > ryan1.2, > ryan2, > ryan2.1.1.1.1.1, > ryan3, > ryan3.2, > > my question/problem is simply this: how do i stop the last comma from > coming? miguel -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] simple array question (Am a Newbie kindly excuse.)
Try this: "); for($i=0; $i < count($a1); $i++) { { echo($a2[$i]); if ($i != (count($a1) - 1)) { echo ","; } } ?> Matthew Walker Senior Software Engineer ePliant Marketing -Original Message- From: r [mailto:[EMAIL PROTECTED]] Sent: Friday, May 10, 2002 3:19 AM To: [EMAIL PROTECTED] Subject: [PHP] simple array question (Am a Newbie kindly excuse.) Greetings my PHP people. Will do things a bit backwork code first and what i want as that seems the easiest way to explain Here is the code: "); while(list($aa1,$aa2) = each ($a1)) { print($aa2 . ","); } ?> Which gives out the output: Tesing arrays: ryan1, ryan1.2, ryan2, ryan2.1.1.1.1.1, ryan3, ryan3.2, my question/problem is simply this: how do i stop the last comma from coming? because originally instead of giving the output to the browser i want it to "feed" it to a hidden form field, which i enter into the database Yeah yeah yeah, wipe the tears of laugher from your eyes and remember, you too were once a newbie. :-o) This list is great and i know i will get not one answer but many, thank you in advance. -Ryan A. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php --- Outgoing mail is certified Virus Free. Checked by AVG anti-virus system (http://www.grisoft.com). Version: 6.0.351 / Virus Database: 197 - Release Date: 4/19/2002 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php