Re: [PHP] delete part of array
Hello Jochem, Wednesday, January 12, 2005, 8:08:09 PM, you wrote: JM read the manual entry first (see below) - and understand what the JM function actually does - never just assume because its giving you JM the result you want now that it will always work the way you JM expect. Don't be a 'tard... Just because someone doesn't explicitly state they didn't read the manual entry first don't assume they didn't because you know what they say about assume. So to make a troll happy, here's exactly what it does with one array given as an argument: If only one array is given and the array is numerically indexed, the keys get reindexed in a continuous way. Wow... Oddly enough that sounds exactly like what he wanted, and hence my suggesting it to him. JM you think??? Now.. On to the part where the I think applies.. Since they do not give an example of a single array being used as an argument, I had to rely on memory from when I needed to do that nearly four months ago. I know I used array_merge(), but I didn't remember if I had to assign it to a variable or not. JM hit the manual: http://www.php.net/array_merge (thats 30 chars to JM type in the addressbar of your favorite browser and then you'd be JM sure) No duh and if you're using Firefox, you can even do something really weird like give the php.net website a keyword (oh, php seems to work nicely), and set your location to http://www.php.net/%s; and amazingly enough you can just type in php array_merge and automagically it takes you right there. That's only 15 chars... Much more efficient than your suggestion. JM probably array_merge() will do what he wants but there maybe JM side-effects that will bite him in the ass later on, same goes for JM my (previous) suggestion of array_values() as it happens blush. His array is numerically indexed. It does exactly what he wants. Cheers, Leif Gregory -- TB Lists Moderator (and fellow registered end-user) PCWize Editor / ICQ 216395 / PGP Key ID 0x7CD4926F Web Site http://www.PCWize.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] delete part of array
use the unset() function. for ($i = 0; $i count($array); $i++) { if (empty($array[$i]) { unset($array[$i]); } } On Wed, 2005-01-12 at 16:22, Sebastian wrote: how do i delete keys from an array if it has no values? eg, this: [name] = Array ( [0] = grape [1] = apple [2] = [3] = orange [4] = [5] = cherry ) to: [name] = Array ( [0] = grape [1] = apple [2] = orange [3] = cherry ) -- Regards, Matthew Fonda -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] delete part of array
that only works for numerical indices. However, if you're sure that neither null values, nor false values are supposed to be present in the array, (that means, they MIGHT be, but should be removed anyway; or just not be there at all,) then you could try array_filter with no callback-argument :) Another easy way would be: foreach($array as $key=$val) { if($val === '') { unset($array[$key]); } } hope it helps - Tul Matthew Fonda wrote: use the unset() function. for ($i = 0; $i count($array); $i++) { if (empty($array[$i]) { unset($array[$i]); } } On Wed, 2005-01-12 at 16:22, Sebastian wrote: how do i delete keys from an array if it has no values? eg, this: [name] = Array ( [0] = grape [1] = apple [2] = [3] = orange [4] = [5] = cherry ) to: [name] = Array ( [0] = grape [1] = apple [2] = orange [3] = cherry ) -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] delete part of array
thanks Leif, not too many think like you :) some people are on this list just because they think they are some genius in php, then when someone with less experience asks for help they bitch and basically tell you to find your own solution or read the manual, that is the first place any REAL php coder would go for help FIRST, but it doesn't always solve a problem thats why there is a thing called MAILING LIST. :) cheers. - Original Message - From: Leif Gregory [EMAIL PROTECTED] To: php-general@lists.php.net Sent: Thursday, January 13, 2005 7:39 AM Subject: Re: [PHP] delete part of array Hello Jochem, Wednesday, January 12, 2005, 8:08:09 PM, you wrote: JM read the manual entry first (see below) - and understand what the JM function actually does - never just assume because its giving you JM the result you want now that it will always work the way you JM expect. Don't be a 'tard... Just because someone doesn't explicitly state they didn't read the manual entry first don't assume they didn't because you know what they say about assume. So to make a troll happy, here's exactly what it does with one array given as an argument: If only one array is given and the array is numerically indexed, the keys get reindexed in a continuous way. Wow... Oddly enough that sounds exactly like what he wanted, and hence my suggesting it to him. JM you think??? Now.. On to the part where the I think applies.. Since they do not give an example of a single array being used as an argument, I had to rely on memory from when I needed to do that nearly four months ago. I know I used array_merge(), but I didn't remember if I had to assign it to a variable or not. JM hit the manual: http://www.php.net/array_merge (thats 30 chars to JM type in the addressbar of your favorite browser and then you'd be JM sure) No duh and if you're using Firefox, you can even do something really weird like give the php.net website a keyword (oh, php seems to work nicely), and set your location to http://www.php.net/%s; and amazingly enough you can just type in php array_merge and automagically it takes you right there. That's only 15 chars... Much more efficient than your suggestion. JM probably array_merge() will do what he wants but there maybe JM side-effects that will bite him in the ass later on, same goes for JM my (previous) suggestion of array_values() as it happens blush. His array is numerically indexed. It does exactly what he wants. Cheers, Leif Gregory -- TB Lists Moderator (and fellow registered end-user) PCWize Editor / ICQ 216395 / PGP Key ID 0x7CD4926F Web Site http://www.PCWize.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] delete part of array
To view the terms under which this email is distributed, please go to http://disclaimer.leedsmet.ac.uk/email.htm On 13 January 2005 05:28, Matthew Fonda wrote: use the unset() function. for ($i = 0; $i count($array); $i++) { if (empty($array[$i]) { unset($array[$i]); } } Without actually running it (I'm too lazy!), I'm pretty sure this won't work correctly -- the count($array) is being recalculated each time round the loop, so each time you do an unset() it will reduce by one, resulting in the last few elements never being checked. It should, however, be perfectly possible to use foreach in the same way: foreach ($array as $i=$value) { if (empty($value]) { unset($array[$i]); } } Cheers! Mike - Mike Ford, Electronic Information Services Adviser, Learning Support Services, Learning Information Services, JG125, James Graham Building, Leeds Metropolitan University, Headingley Campus, LEEDS, LS6 3QS, United Kingdom Email: [EMAIL PROTECTED] Tel: +44 113 283 2600 extn 4730 Fax: +44 113 283 3211 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] delete part of array
crap - hit 'reply' by mistake. --- Leif Gregory wrote: Hello Jochem, Wednesday, January 12, 2005, 8:08:09 PM, you wrote: JM read the manual entry first (see below) - and understand what the JM function actually does - never just assume because its giving you JM the result you want now that it will always work the way you JM expect. Don't be a 'tard... Just because someone doesn't explicitly state they heh but who says I'm not a bar steward ;-) didn't read the manual entry first don't assume they didn't because you know what they say about assume. sorry if it came accross a little harsh, but I stand by the point that the onus is on the person asking the question to give an indication as to what he has done to try and help him/her-self - spoonfeeding is for babies, not programmers. So to make a troll happy, here's exactly what it does with one array given as an argument: If only one array is given and the array is numerically indexed, the keys get reindexed in a continuous way. happy troll here :-) Wow... Oddly enough that sounds exactly like what he wanted, and hence my suggesting it to him. JM you think??? Now.. On to the part where the I think applies.. Since they do not give an example of a single array being used as an argument, I had to rely on memory from when I needed to do that nearly four months ago. I know I used array_merge(), but I didn't remember if I had to assign it to a variable or not. so my point stands - if your going to help why not grab the nearest shell and test by way of a php -r 'test-code'? takes just along as writing both versions in the mail and makes _you_ look better IMHO. JM hit the manual: http://www.php.net/array_merge (thats 30 chars to JM type in the addressbar of your favorite browser and then you'd be JM sure) No duh and if you're using Firefox, you can even do something really weird like give the php.net website a keyword (oh, php seems to work nicely), and set your location to http://www.php.net/%s; and amazingly enough you can just type in php array_merge and automagically it takes you right there. That's only 15 chars... Much more efficient than your suggestion. indeed it is! thanks for the tip :-) , I might add though that people choose a close/fast mirror rather than www.php.net itself to help offload some of the traffic - my preferred mirror is always way faster the www.php.net, which is a nice when your hitting the manual 20+ times a day. BTW: took me a few mins to figure out where to set this: in the bookmarks details dialog (via the bookmark manager - and possibly other routes) if anyone else is interested. JM probably array_merge() will do what he wants but there maybe JM side-effects that will bite him in the ass later on, same goes for JM my (previous) suggestion of array_values() as it happens blush. His array is numerically indexed. It does exactly what he wants. I personally couldn't tell what exactly he wanted to filter out of his array (false, '' or NULL) or whether he cared about the difference in this case. I was merely trying to encourage porper understanding of the function rather than using something which works under the current conditions Cheers, Leif Gregory -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] delete part of array
Ford, Mike wrote: snip Without actually running it (I'm too lazy!), I'm pretty sure this won't work correctly -- the count($array) is being recalculated each time round the loop, so each time you do an unset() it will reduce by one, resulting in the last few elements never being checked. /snip Not too much on this topic, but to save youself a few cpu cycles, you could put your size outside of the for loop $size = sizeof ( $array ); for ( $i = 0; $i $size; $i++ ) Putting it inside the for() makes it call that function on every loop. -- John C. Nichel ÜberGeek KegWorks.com 716.856.9675 [EMAIL PROTECTED] -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] delete part of array
Hello Jochem, Thursday, January 13, 2005, 10:55:35 AM, you wrote: J heh but who says I'm not a bar steward ;-) grin I'll take a beer then! I need one after today. J sorry if it came accross a little harsh, but I stand by the point J that the onus is on the person asking the question to give an J indication as to what he has done to try and help him/her-self - J spoonfeeding is for babies, not programmers. I apologize too. Here I am, the moderator and owner of five other lists harping on my users to not reply too quickly, and here I go and do it. Isn't that one of the moderator credos though? Do as I say, not as I do? :-) I agree that handing someone the silver platter every time they ask isn't the right way to do it, but I don't mind sharing what I've learned either. J so my point stands - if your going to help why not grab the nearest J shell and test by way of a php -r 'test-code'? takes just along J as writing both versions in the mail and makes _you_ look better J IMHO. True 'nuff. J indeed it is! thanks for the tip :-) , np :-) J I might add though that people choose a close/fast mirror rather J than www.php.net itself to help offload some of the traffic - my J preferred mirror is always way faster the www.php.net, which is a J nice when your hitting the manual 20+ times a day. I can agree with that. I'll fix mine. J BTW: took me a few mins to figure out where to set this: in the J bookmarks details dialog (via the bookmark manager - and possibly J other routes) if anyone else is interested. Do you reckon you can handle two I think's is a 24 hour period? grin I think you can do this in IE too, but it's more difficult. When I used to use IE way back when, I think I set up google as my search engine instead of MSN and did something like that. It coulda all been a dream too... :-) J I personally couldn't tell what exactly he wanted to filter out of J his array (false, '' or NULL) or whether he cared about the J difference in this case. I was merely trying to encourage porper J understanding of the function rather than using something which J works under the current conditions I wasn't quite sure either but thought I'd give it a shot. At any rate, getting OT. Talk at you later. It's time to go home and play with the baby's mama. -- Leif (TB lists moderator and fellow end user). Using The Bat! 3.0.2.3 Rush under Windows XP 5.1 Build 2600 Service Pack 2 on a Pentium 4 2GHz with 512MB -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] delete part of array
how do i delete keys from an array if it has no values? eg, this: [name] = Array ( [0] = grape [1] = apple [2] = [3] = orange [4] = [5] = cherry ) to: [name] = Array ( [0] = grape [1] = apple [2] = orange [3] = cherry ) -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] delete part of array
Sebastian wrote: how do i delete keys from an array if it has no values? eg, this: [name] = Array ( [0] = grape [1] = apple [2] = [3] = orange [4] = [5] = cherry ) to: [name] = Array ( [0] = grape [1] = apple [2] = orange [3] = cherry ) foreach ($name as $value) { if ($value) $buffer[] = $value; } $name = $buffer; -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] delete part of array
Hello Sebastian, Wednesday, January 12, 2005, 5:22:20 PM, you wrote: S how do i delete keys from an array if it has no values? S eg, this: array_merge($name); Try it that way first. If not, $name = array_merge($name); But I think I remember the first way working for me. Cheers, Leif Gregory -- TB Lists Moderator (and fellow registered end-user) PCWize Editor / ICQ 216395 / PGP Key ID 0x7CD4926F Web Site http://www.PCWize.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] delete part of array
Sebastian wrote: how do i delete keys from an array if it has no values? eg, this: what does the value have to do with it??? other than that you want to remove items where the value is 'empty'. also 'no value' is vague, do you mean an empty string or do you mean NULL? --- try the array_values() function, possibly in conjunction with array_unique() depending on your requirements. look them up in the manual, while your there read the rest of it as well. --- I assume you know how to: 1. create an array 2. loop an array 3. check a variable's value is/isn't something in particular 4. assign a value to a variable. combine these skills! (if you can answer no to any of the above questions then you need to read the manual - its a good manual, people have spent countless hours writing - show respect and use it!) [name] = Array ( [0] = grape [1] = apple [2] = [3] = orange [4] = [5] = cherry ) to: [name] = Array ( [0] = grape [1] = apple [2] = orange [3] = cherry ) -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] delete part of array
Leif Gregory wrote: Hello Sebastian, Wednesday, January 12, 2005, 5:22:20 PM, you wrote: S how do i delete keys from an array if it has no values? S eg, this: array_merge($name); Try it that way first. If not, read the manual entry first (see below) - and understand what the function actually does - never just assume because its giving you the result you want now that it will always work the way you expect. $name = array_merge($name); But I think I remember the first way working for me. you think??? hit the manual: http://www.php.net/array_merge (thats 30 chars to type in the addressbar of your favorite browser and then you'd be sure) probably array_merge() will do what he wants but there maybe side-effects that will bite him in the ass later on, same goes for my (previous) suggestion of array_values() as it happens blush. a tip for those who don't already know: php.net (and its mirrors (I usually use nl2.php.net)) have a great search mechanism: http://www.php.net/function-name replace function-name with the name of function your interested in and boom your right at the page you need! actually other keywords work as well. for instance: need to know more about arrays: http://www.php.net/array or mysql http://www.php.net/mysql or overloading http://www.php.net/overload even when you don't know the exact keyword to use, your guess will often get you where you want! BLATANT-PROMO nl2.php.net is the best php mirror out there *obviously*, nothing to do with the fact that the guys that run it are friends of mine, or the fact that they do such a great job at hosting alot of the sites I build/run/own ;-) [www.nedlinux.nl: linux freaks with a lowlands twist] /BLATANT-PROMO Cheers, Leif Gregory -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php