Programming/PHP2014. 9. 4. 15:43


Anonymous functions - http://php.net/manual/ro/functions.anonymous.php


있다는 것만 알지.. 사용하지 않았다.. 

그런데 구글링 중 괜찮은 활용 방법을 찾아서!! 왠지 jquery 하는듯한 느낌은 뭐지...;;

 - 특정 배열값 파싱하거나 재구성할때 for문 안돌리고 array_map 이용할때 괜찮은듯함.

예) http://stackoverflow.com/questions/13435747/how-can-i-remove-the-file-extension-from-each-string-in-a-php-array


  1. <?php
  2. # your array
  3. $slike = array('1.jpg','253455.jpg','32.jpg','477.jpg');
  4. # if you have PHP >= 5.3, I'd use this
  5. $slike = array_map(function($e){
  6. return pathinfo($e, PATHINFO_FILENAME);
  7. }, $slike);
  8. # if you have PHP <= 5.2, use this
  9. $slike = array_map('pathinfo', $slike, array_fill(
  10. 0, count($slike), PATHINFO_FILENAME
  11. ));
  12. # dump
  13. print_r($slike);
  14. /*print
  15. Array
  16. (
  17. [0] => 1
  18. [1] => 253455
  19. [2] => 32
  20. [3] => 477
  21. )
  22. */
  23. ?>


※ 단 PHP 5.3 이상부터 가능함!

Posted by 시니^^