logs archiveIRC Archive / Freenode / #php / 2015 / August / 13 / 1
whoever
hi all, if I want to clear an array for later use, is there a more efficient way then reinitializing it ie $foo = array(); ?
x0rs
I have a file in which this line appears a number of times: "DTSTART:20160312Z" I need to capture the date between "DTSTART:" and "Z" each time this line appears and replace it with datetime like: "20160312T230000" - suggestions on the best way to do this?
Sheraf
regex
or explode
whoever
+1 Sheraf
Sheraf
$foo = array(); sounds pretty efficient to me
whoever
i think explode would just look ugly , but would do it
x0rs
sheraf, regex was my thought, but I wasn't sure if I needed to walk the file line by line, or if these was a way to preg_replace the whole file with the new date() value.
whoever
Sheraf: it just seems so primative
Sheraf
explode would probably be faster
         

whoever
x0rs: well yes you do
while file has next , to get each line so you are reading the file
x0rs
whoever, thank you
Dustin|Work
Sheraf, you can also do unset($array);
Alphos
x0rs !+sscanf
php-bot
x0rs, mixed sscanf(string str, string format [, string ...]) - Implements an ANSI C compatible sscanf - http://php.net/sscanf
Alphos
x0rs and DateTime, afterwards
x0rs
hmm, thanks Alphos
Dustin|Work
Erm that was meant for whoever
Alphos
x0rs DateTime even has DateTime::W3C to make your life easier when handling iso dates
(outputting them)
whoever
x0rs: i just agreed with Sheraf :-) so what are you thanking me for
Sheraf: what is the purpose of unset() ? because if i understand it correctly the variable will be removed from memory anyway
Alphos
whoever it unsets the variable
you don't need to unset a variable you won't be using, but you do if you are expecting it to be null again at some point in your script
Sheraf
unset will erase the variable
but i think you want to reset it to an empty array?
whoever
Alphos: wouldn't unset do the same as reinitializing than ?
Alphos
whoever no, reinitializing sets to a given value. that value might be empty, but it is a value. unset actually unsets the variable
$
whoever
ok
thx
Alphos
using a "reinitialized" value won't trigger any error, per se. using an unset variable will trigger an E_NOTICE-level error
whoever
the docs say unset destroys, and i thaught remove even the var instance from mem
so thats whaere my confusion started
Dustin|Work
I did a test, if you initialize an array, set an array's values, then unset the array, then reapply some array values, it works fine
Alphos
whoever you can always "re"set an unset variable
whoever
ok, is there another way to get the contentts of an array other than pritr is there something like implode
that i could use
         

TML
whoever: What are you trying to do?
whoever
TML: output an array without a for loop or printr, because i want the contents without the data type
biberu
this begs for a "why?"
TML
whoever: foreach($array as $item) echo $item;
done
Alphos
i'm with biberu on this one. "why ?" indeed
wfq
d
biberu
specifically not a for is an odd requirement, is it really just that? homework for loop constructs?
Rayn27
I find counters more useful than foreach loops, personally.
caffinated
I don't
Rayn27
Why not?
caffinated
Because most of the time you don't really need an iterator.
Rayn27
Ahhh, I see.
That is true.
Alphos
and in cases you do, sometimes you're stuck with non-sequential keys
caffinated
also, there are distinct differences between looping an array using for, vs foreach.
Alphos
and/or the key isn't numeric
caffinated
the most important being, what happens if you change the array you're looping.
Rayn27
caffinated, what sort of differences?
Alphos
for has its usecase, foreach has its usecase too, they're not interchangeable
caffinated +1
Rayn27 $a = ['foo']; foreach ($a as $v ) { $a[] = 'bar'; }// you end up with $a === ['foo', 'bar']
whereas $a = ['foo']; for ($i = 0 ; $i < count($a) ; $i++ ) { $a[] = 'bar'; } // you don't end up with anything, this is an infinite loop
Rayn27
Ahh, I see.
caffinated
realistically only infinite if you constantly enumerate count(). but the point here is that it adds a potential for unexpected behavior.
Rayn27
Glad to learn the nuances between the two.
TML
I prefer foreach just because it removes the chance to make fencepost errors
Alphos
by the way, don't ever ever EVER put count($array) in the condition of the for that loops over a given array
Rayn27
Alphos, why not?
xyphoid
foreach <3
Alphos
Rayn27 because of the example you were just given
TML
Rayn27: look at caffinated's example again
Rayn27
Oh. Duh. Right.
Alphos
granted, every rule has its exceptions, sometimes you're safe, but better to be always safe than "sometimes safe"
caffinated
Rayn27: commonly you don't want to continually re-evaluate something that will (should) not change over and over.
Rayn27
(Action) nods
TML
$cnt = count(...); for($i=0; $i < $cnt; $i++){ ... }
Alphos
exactly
TML
it's also cheaper
caffinated
in the case of what alphos is telling you, it's a bit stickier in that re-evaluating the count of an array you're modifying will constantly make the end a moving target.
TML
you don't pay the calculation cost every loop
Alphos
IF you actually want to know the number of elements of your array AT ANY GIVEN TIME, and you have a break in your for, you MAY count inside your conditional
you usually don't need to, just assign a temporary variable to the value before the for loop, and evaluate your conditional against that
but for loops aren't really all that useful for arrays :/
if you already have the array, you don't need to generate another arrayish thing and loop over that ; just use the array you have
caffinated
even in cases where I'd want to loop a segment of an array by position, i'd use array_slice()
and then foreach() the slice.
Alphos
or array_chunk
gremlyn
is it reasonable to use Exception's code parameter to store http status codes?
Alphos
gremlyn not really, no
gremlyn
ok, so why not?
Alphos
exception code is something internal, http status code is transmitted over the wire to the client
caffinated
gremlyn: if you want to include expanded information with an exception, extend Exception in to a custom type.
Alphos
exception code is something to help you debug, not something to transmit information outside
gremlyn
hmm
Alphos
as a general rule, exceptions aren't there to broadcast anything
caffinated
class HttpException extends Exception { public $http_status = ''; }
TML
Alphos: I think he might mean "If reading from an HTTP response throws an exception, is there a problem with re-using the HTTP exception as the 'exception code'"
s/HTTP exception/HTTP response code/
gremlyn
I'm building some ajax-based routes that rely on status codes to transmit success/failure
Alphos
TML even then, i wouldn't be too fond of it :/
« prev 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 next »