List comprehensions for PHP

  • spork
  • Brewmaster
  • Silver Member
  • User avatar
  • Posts: 6251
  • Loc: Seattle, WA

Post 3+ Months Ago

If you're used to the convenience and conciseness of list comprehensions in languages such as Python, you might be interested in php-lc, which provides a means for specifying comprehensions as strings to manipulate PHP arrays:

http://code.google.com/p/php-lc/

Example from the project page:
PHP Code: [ Select ]
$Foo = array (1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
print_r (lc ('pow ($i, 2) for $i in $Foo if $i % 2', compact ('Foo')));
  1. $Foo = array (1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
  2. print_r (lc ('pow ($i, 2) for $i in $Foo if $i % 2', compact ('Foo')));

result:
Code: [ Select ]
Array
(
  [0] => 1
  [1] => 9
  [2] => 25
  [3] => 49
  [4] => 81
)
  1. Array
  2. (
  3.   [0] => 1
  4.   [1] => 9
  5.   [2] => 25
  6.   [3] => 49
  7.   [4] => 81
  8. )


Not the best, but a decent solution if moving to a different language is out of the question.
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

Post Information

  • Total Posts in this topic: 1 post
  • Users browsing this forum: No registered users and 158 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
cron
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.