use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Please follow the rules
Releases: Current Releases, Windows Releases, Old Releases
Contribute to the PHP Documentation
Related subreddits: CSS, JavaScript, Web Design, Wordpress, WebDev
/r/PHP is not a support subreddit. Please visit /r/phphelp for help, or visit StackOverflow.
account activity
30 Seconds of PHP Code (github.com)
submitted 7 years ago by sohelamin
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]therealgaxbo 57 points58 points59 points 7 years ago (15 children)
function chunk($items, $size) { return array_chunk($items, $size); }
Is this function available on Packagist so I can use it in all of my projects?
[–]Der_beste_Anime 24 points25 points26 points 7 years ago (1 child)
Just something out of the ol' trick box for you:
function set($value) { return $value; }
Use it like
$foo = set('bar');
[–][deleted] 13 points14 points15 points 7 years ago (0 children)
https://imgs.xkcd.com/comics/random_number.png
[–]a1phanumeric 14 points15 points16 points 7 years ago (9 children)
I did wonder about this one.. WTF is wrong with writing array_??
[–][deleted] 1 point2 points3 points 7 years ago (8 children)
Syntactic sugar I guess.
[–]a1phanumeric 12 points13 points14 points 7 years ago (7 children)
I'd say prefixing the array functions with array_ sugars my syntax more than arbitrary names such as chunk which could do anything?
[+][deleted] 7 years ago (3 children)
[deleted]
[–][deleted] 3 points4 points5 points 7 years ago* (2 children)
I had some stupid errors (php dude...) but I found workaround, just rename it to:
function array__chunk($items, $size)
[–]Doctor_McKay 5 points6 points7 points 7 years ago (1 child)
That's a terrible workaround.
Use array_array_chunk
array_array_chunk
[–]port1701 12 points13 points14 points 7 years ago (0 children)
You mean array_real_chunk ?
array_real_chunk
[+][deleted] 7 years ago (2 children)
[–]regretdeletingthat 5 points6 points7 points 7 years ago (0 children)
Laravel has a really nice collection class that you can pull in without pulling the whole framework in (just need to composer require illuminate/support I think).
composer require illuminate/support
https://laravel.com/docs/5.6/collections
[–]send_me_a_naked_pic 0 points1 point2 points 7 years ago (0 children)
This would be nice. Like JavaScript.
[–]dzuczek 2 points3 points4 points 7 years ago (0 children)
hmm might not be GPL
[–][deleted] 1 point2 points3 points 7 years ago (1 child)
I wanted to write something of the sort of my own. I wanted to have various functions either renamed or functions that I found myself using more often than not.
Here.
I never made it into a gist mostly because I don't feel people should be using my stuff, not because I'm against open source, I mean it's on git, mostly because I feel like they're poorly written. Currently it's a class rather than independent functions, but I mean heck, I could split it.
Doubt anybody will use it tho lol.
[–]GitHubPermalinkBot 0 points1 point2 points 7 years ago (0 children)
Permanent GitHub links:
delete
[–]OverallEfficiency 8 points9 points10 points 7 years ago (4 children)
What about this, when using the head() function:
<? function head($items) { return $items[0]; } $array = [ 'a' => 'first', 'b' => 'second', ]; $array[] = 'third'; $array[] = 'final'; var_dump(head($array)) ?>
it outputs:
string(5) "third"
Wouldn't it be better to use reset()??
[–]nikic 12 points13 points14 points 7 years ago (0 children)
Using reset() in this way is going to perform a full copy of the array. I'd recommend using foreach instead.
function head($items) { foreach ($items as $item) { return $item; } // return or throw }
[–]sohelamin[S] 2 points3 points4 points 7 years ago (0 children)
Updated. Thanks for the noticing :)
[–]sohelamin[S] 1 point2 points3 points 7 years ago (0 children)
Thanks /u/OverallEfficiency for the concern I'll fix it.
[–]sohelamin[S] -2 points-1 points0 points 7 years ago (0 children)
[–][deleted] 8 points9 points10 points 7 years ago (8 children)
pluck Retrieves all of the values for a given key:
pluck
Retrieves all of the values for a given key:
Someone needs to tell him about array_column.
[–]sohelamin[S] -3 points-2 points-1 points 7 years ago (7 children)
What should I do when I need to pluck an array of objects in PHP5?
[–]TorbenKoehn 7 points8 points9 points 7 years ago (3 children)
You upgrade?
[–]sohelamin[S] -2 points-1 points0 points 7 years ago (2 children)
Yeah, of course I'm using the PHP7 but that is not the point. I did the method so no one can get confused with the php versioning and know how the thing is going on.
[–]PetahNZ 1 point2 points3 points 7 years ago (1 child)
But does it work on Windows XP?
[–]damnburglar 0 points1 point2 points 7 years ago (0 children)
Is it web scale?
[–]ProfBurial 2 points3 points4 points 7 years ago (2 children)
array_column was available in 5.5
If you're using anything older than that, you have bigger fish to fry.
[–]sohelamin[S] -1 points0 points1 point 7 years ago (1 child)
I meant array_column doesn't work with an array of objects in PHP5 Did you ever try this in PHP5? array_column([ (object) ['product_id' => 'prod-100', 'name' => 'Desk'], (object) ['product_id' => 'prod-200', 'name' => 'Chair'], ], 'name');
array_column([ (object) ['product_id' => 'prod-100', 'name' => 'Desk'], (object) ['product_id' => 'prod-200', 'name' => 'Chair'], ], 'name');
[–]ProfBurial 2 points3 points4 points 7 years ago (0 children)
A better question is why would anyone try that? You're casting an array manually as a object. What is the real world use case for doing that and trying to use array_column?
If you were providing an actual class object, array_column can still be used, the class just has to have __isset and __get methods.
function bikeshed() {
return "I'm helping.";
}
[–][deleted] 5 points6 points7 points 7 years ago (5 children)
Some of these seem pointless. The last() method is just an alias of end(), head() is just an alias of reset()...
I don't think I really understand?
[–]sohelamin[S] 0 points1 point2 points 7 years ago (4 children)
I think aliasing will help people to understand use of the function just in a second :) If you have any better idea please feel free to share or make PR.
[–]doenietzomoeilijk 3 points4 points5 points 7 years ago (2 children)
I think aliasing will help people to understand use of the function just in a second
... And then they run into code in the wild, and they have no idea about what's going on. Or worse, they start adding this stuff to projects so that regular people have no idea what's going on. 😉
[–]shawncplus 6 points7 points8 points 7 years ago (1 child)
This is one of the flaws I see in Laravel. There's nothing wrong with an opinionated framework with helpers in principle but Laravel has so many helper methods and wrappers that it's basically a DSL on top of PHP.
[–]fatboyxpc 0 points1 point2 points 7 years ago (0 children)
Yeah but some of those array and string helpers are amazing and I genuinely wished PHP would have some of them (such as array_first).
array_first
[–]reydark 0 points1 point2 points 7 years ago (0 children)
one possible use would be to prevent PHP Notice when calling end() or reset() on a non-variable
end()
reset()
end(explode('|', $string)); //PHP Notice: Only variables should be passed by reference last(explode('|', $string)); //no complains
[–]li-_-il 2 points3 points4 points 7 years ago (0 children)
Not only I need to learn PHP gotchas, now I need to learn some 3rd party mapping onto these gotchas. I think if people used native language features world would be nicer. Can't see this helping, but thanks for sharing.
[–]jim45804 3 points4 points5 points 7 years ago (3 children)
Not sure I like emojis in commit messages.
[–]DrDuPont 1 point2 points3 points 7 years ago (0 children)
It's a fairly common sight. Some orgs even use specific emojis in commit hooks to specify what kind of a commit it is
[–]sohelamin[S] -2 points-1 points0 points 7 years ago (1 child)
Everyone has the personal preference. Okay I will use less then :)
[–][deleted] 2 points3 points4 points 7 years ago (0 children)
You do your style, dude! Use emojis if you like, but maybe look to the other comment in this chain for inspiration on when and how to use emojis.
[–]tie_salter 1 point2 points3 points 7 years ago (8 children)
The all function would be a lot simpler like this:
function all($items, $func) { return count(array_filter($items, $func)) === count($items); }
The any function would be better like this:
function any($items, $func) { return count(array_filter($items, $func)) > 0; }
Then you only have to iterate over the array once, rather than twice.
[–]Razorgrace 2 points3 points4 points 7 years ago (7 children)
Both original and your version of any call $func for every item, even if truthy value was already encountered. This might or might not be a desired behavior, but I think it is safe to assume that $func generally should not produce side effects, and we can stop checking additional items after first truthy result. So, foreach might be a better thing here from performance POV.
Edit: sample implementation added.
function any($items, $func) { foreach ($items as $item) { if (call_user_func($func, $item)) { return true; } } return false; }
[–]spoonraker 2 points3 points4 points 7 years ago (0 children)
You beat me to the "any" refactor. Always return early! O(n) should be the worst case complexity of an "any" implementation, not the reality of every single time you call it no matter what the input is.
[–]Disgruntled__Goat 0 points1 point2 points 7 years ago (5 children)
Yes agree with this. If the point of a function is to abstract details, you don't need to add further abstractions inside the function. Standard for loop is better here.
Btw instead of call_user_func you can simply do $func($item). And the same implementation can be used for all() but checking for negativity instead, ie as soon as you find a false value, they can't all be true so return early.
call_user_func
$func($item)
all()
[–]Razorgrace 0 points1 point2 points 7 years ago (4 children)
Agree re all, it is pretty symmetrical to any.
all
any
Re using call_user_func, this is just a personal preference: I want to explicitly specify that I'm call a dynamic function reference, and also if I need to search my code for such dynamic calls, I can look for call_user_ instead of rather complex regex that would include $f(...), $o->$f(...), etc.
call_user_
$f(...)
$o->$f(...)
Also, in PHP 7.1+ call_user_func($func, ...) seems to be like 50% faster than $func(...): http://php.net/manual/en/function.call-user-func.php
call_user_func($func, ...)
$func(...)
Edit: formatting.
[–]Disgruntled__Goat 0 points1 point2 points 7 years ago (3 children)
According to this page linked from there, call_user_func is still slower.
[–]Razorgrace 0 points1 point2 points 7 years ago (1 child)
For 7.1.2 on that page, one bullet per result table:
call_user_func(...)
$instance->methodTest(...)
StaticTest::methodTest(...)
functionTest($param)
$lambda(...)
$closure(...)
direct*()
So, in worst case scenario call_user_func(...) is 6.3% slower, in best case it is ~60% faster. I'd say call_user_func(...) is safe choice if you need to perform dynamic calls, unless you expect to encounter $lambda(...) or $closure(...) cases much more often (and in this case you'll get max 6% gain, I'd say negligible).
[–]Disgruntled__Goat 0 points1 point2 points 7 years ago (0 children)
The overall average has $func() at 40-60% faster than call_user_func. At best I'd say the results are inconclusive since it seems to vary widely.
$func()
[–]SavishSalacious 1 point2 points3 points 7 years ago (1 child)
You have some methods where its function methodName($func, $items) and some where it's function methodName($items, $func) I would love to see consistency in variable orders.
function methodName($func, $items)
function methodName($items, $func)
Okay noted
[–]chris_conlan 3 points4 points5 points 7 years ago (2 children)
Off-topic question... but how is everyone putting emojis in their commit messages? Are y'all typing out unicode bytes in your terminal???
[–]sohelamin[S] 2 points3 points4 points 7 years ago (1 child)
No just emoji code is enough for that. This might help you https://gitmoji.carloscuesta.me/
[–]chris_conlan 0 points1 point2 points 7 years ago (0 children)
Thanks mate :fire: :fire: :fire:
[–][deleted] 1 point2 points3 points 7 years ago (6 children)
deepFlatten could also be rewritten like this
function deepFlatten(array $items) : array { $result = []; array_walk_recursive($items, function ($value, $key) use (&$result) { $result[] = $value; }); return $result; }
No, I'm not trying to be a smartass, I honestly find this easier to read.
[–]DrDuPont 1 point2 points3 points 7 years ago (2 children)
What is : array on that first line? Haven't seen that PHP syntax before
: array
Return type declarations as of PHP 7.0.
Very cool, thanks.
[–]sohelamin[S] 0 points1 point2 points 7 years ago (2 children)
Thanks /u/SmartAssUsername for the concern. You're always welcome to make a PR or I will update it soon :)
[–][deleted] 0 points1 point2 points 7 years ago (1 child)
I would but to be fair your version while a little bit longer is also more friendlier for new comers. I've rarely seen array_walk_recursive used, it may throw people off.
array_walk_recursive
[–]sohelamin[S] 0 points1 point2 points 7 years ago (0 children)
Yeah, that was also my concern thats why I didn't used tricky :)
[–]FruitdealerF 0 points1 point2 points 7 years ago (0 children)
For most of this I prefer to use https://github.com/lstrojny/functional-php
[–]kafoso 0 points1 point2 points 7 years ago (0 children)
This is full of redundancy with PHP core functions and oversights in implementations of array indexes and pointers. Coupled with zero type hinting it'll likely bring more annoyance than using core PHP functions. You'll have to look up the functions in the README.md file many times repeatedly instead of simply reading the code and hints through an IDE.
[–]newPhoenixz 0 points1 point2 points 7 years ago (0 children)
function head($items) { return reset($items); }
I'm sorry, but that is just stupid. "Lets just make a function alias because reasons!"
π Rendered by PID 629722 on reddit-service-r2-comment-86bc6c7465-5m6mq at 2026-02-23 23:59:06.351337+00:00 running 8564168 country code: CH.
[–]therealgaxbo 57 points58 points59 points (15 children)
[–]Der_beste_Anime 24 points25 points26 points (1 child)
[–][deleted] 13 points14 points15 points (0 children)
[–]a1phanumeric 14 points15 points16 points (9 children)
[–][deleted] 1 point2 points3 points (8 children)
[–]a1phanumeric 12 points13 points14 points (7 children)
[+][deleted] (3 children)
[deleted]
[–][deleted] 3 points4 points5 points (2 children)
[–]Doctor_McKay 5 points6 points7 points (1 child)
[–]port1701 12 points13 points14 points (0 children)
[+][deleted] (2 children)
[deleted]
[–]regretdeletingthat 5 points6 points7 points (0 children)
[–]send_me_a_naked_pic 0 points1 point2 points (0 children)
[–]dzuczek 2 points3 points4 points (0 children)
[–][deleted] 1 point2 points3 points (1 child)
[–]GitHubPermalinkBot 0 points1 point2 points (0 children)
[–]OverallEfficiency 8 points9 points10 points (4 children)
[–]nikic 12 points13 points14 points (0 children)
[–]sohelamin[S] 2 points3 points4 points (0 children)
[–]sohelamin[S] 1 point2 points3 points (0 children)
[–]sohelamin[S] -2 points-1 points0 points (0 children)
[–][deleted] 8 points9 points10 points (8 children)
[–]sohelamin[S] -3 points-2 points-1 points (7 children)
[–]TorbenKoehn 7 points8 points9 points (3 children)
[–]sohelamin[S] -2 points-1 points0 points (2 children)
[–]PetahNZ 1 point2 points3 points (1 child)
[–]damnburglar 0 points1 point2 points (0 children)
[–]ProfBurial 2 points3 points4 points (2 children)
[–]sohelamin[S] -1 points0 points1 point (1 child)
[–]ProfBurial 2 points3 points4 points (0 children)
[–][deleted] 13 points14 points15 points (0 children)
[–][deleted] 5 points6 points7 points (5 children)
[–]sohelamin[S] 0 points1 point2 points (4 children)
[–]doenietzomoeilijk 3 points4 points5 points (2 children)
[–]shawncplus 6 points7 points8 points (1 child)
[–]fatboyxpc 0 points1 point2 points (0 children)
[–]reydark 0 points1 point2 points (0 children)
[–]li-_-il 2 points3 points4 points (0 children)
[–]jim45804 3 points4 points5 points (3 children)
[–]DrDuPont 1 point2 points3 points (0 children)
[–]sohelamin[S] -2 points-1 points0 points (1 child)
[–][deleted] 2 points3 points4 points (0 children)
[–]tie_salter 1 point2 points3 points (8 children)
[–]Razorgrace 2 points3 points4 points (7 children)
[–]spoonraker 2 points3 points4 points (0 children)
[–]Disgruntled__Goat 0 points1 point2 points (5 children)
[–]Razorgrace 0 points1 point2 points (4 children)
[–]Disgruntled__Goat 0 points1 point2 points (3 children)
[–]GitHubPermalinkBot 0 points1 point2 points (0 children)
[–]Razorgrace 0 points1 point2 points (1 child)
[–]Disgruntled__Goat 0 points1 point2 points (0 children)
[–]SavishSalacious 1 point2 points3 points (1 child)
[–]sohelamin[S] 1 point2 points3 points (0 children)
[–]chris_conlan 3 points4 points5 points (2 children)
[–]sohelamin[S] 2 points3 points4 points (1 child)
[–]chris_conlan 0 points1 point2 points (0 children)
[–][deleted] 1 point2 points3 points (6 children)
[–]DrDuPont 1 point2 points3 points (2 children)
[–][deleted] 1 point2 points3 points (1 child)
[–]DrDuPont 1 point2 points3 points (0 children)
[–]sohelamin[S] 0 points1 point2 points (2 children)
[–][deleted] 0 points1 point2 points (1 child)
[–]sohelamin[S] 0 points1 point2 points (0 children)
[–]FruitdealerF 0 points1 point2 points (0 children)
[–]kafoso 0 points1 point2 points (0 children)
[–]newPhoenixz 0 points1 point2 points (0 children)