Php function set-php Tutorial

Source: Internet
Author: User
Php function set
  • Count the number of array elements
  • php$arr = array(      '1011,1003,1008,1001,1000,1004,1012',      '1009',      '1011,1003,1111'    );$result = array();foreach ($arr as $str) {  $str_arr = explode(',', $str);  foreach ($str_arr as $v) {    // $result[$v] = isset($result[$v]) ? $result[$v] : 0;    // $result[$v] = $result[$v] + 1;    $result[$v] = isset($result[$v]) ? $result[$v]+1 : 1;  }}

    Print_r ($ result );
    // Array
    (
    [1011] => 2
    [1003] => 2
    [1008] => 1
    [1001] => 1
    [1000] => 1
    [1004] => 1
    [1012] => 1
    [1009] => 1
    [1111] => 1
    )
    2. delete directories cyclically

    phpfunction cleanup_directory($dir) {  foreach (new DirectoryIterator($dir) as $file) {    if ($file->isDir()) {      if (! $file->isDot()) {        cleanup_directory($file->getPathname());      }    } else {       unlink($file->getPathname());    }  }   rmdir($dir);}

    3. Infinitus classification spanning tree

    Php
    Function generateTree ($ items) {$ tree = array (); foreach ($ items as $ item) {if (isset ($ items [$ item ['pid']) {$ items [$ item ['pid '] ['son'] [] = & $ items [$ item ['id'];} else {$ tree [] = & $ items [$ item ['id'] ;}} return $ tree;} function generateTree2 ($ items) {foreach ($ items as $ item) $ items [$ item ['pid '] ['son'] [$ item ['id'] = & $ items [$ item ['id']; return isset ($ items [0] ['son'])? $ Items [0] ['son']: array () ;}$ items = array (1 => array ('id' => 1, 'pid '=> 0, 'name' => 'Anhui province '), 2 => array ('id' => 2, 'pid' => 0, 'name' => 'Zhejiang province '), 3 => array ('id' => 3, 'pid '=> 1, 'name' => 'Hefei '), 4 => array ('id' => 4, 'pid '=> 3, 'name' => 'changfeng County '), 5 => array ('id' => 5, 'pid '=> 1, 'name' => 'anqing'),); print_r (generateTree ($ items )); /*** how to format the tree data */$ tree = generateTree ($ items); function getTreeData ($ tree) {foreach ($ tree as $ t) {echo $ t ['name'].'
    '; If (isset ($ t ['son']) {getTreeData ($ t ['son']) ;}}

    4. array sorting a-B is required when writing a numeric array into a string.

    phpvar test = ['ab', 'ac', 'bd', 'bc'];test.sort(function(a, b) {    if(a < b) {        return -1;    }    if(a > b) {        return 1;    }    return 0;});
  • Array_reduce
  • Php $ raw = [1, 2, 3, 4, 5,]; // The third parameter of array_reduce is the initial value of $ result array_reduce ($ raw, function ($ result, $ value) {$ result [$ value] = $ value; return $ result;}, []); // [1 => 1, 2 => 2 ,... 5 => 5]

    6. only one or more parameters are accepted in the array_map closure. the number of closure parameters must be the same as that of the array_map parameter.

    php$input = ['key' => 'value'];array_map(function($key, $value) {    echo $key . $value;}, array_keys($input), $input)// 'keyvalue'

    7. Breed Rabbits

    Php $ month = 12; $ fab = array (); $ fab [0] = 1; $ fab [1] = 1; for ($ I = 2; $ I <$ month; $ I ++) {$ fab [$ I] = $ fab [$ I-1] + $ fab [$ I-2];} for ($ I = 0; $ I <$ month; $ I ++) {echo sprintf ("Rabbit in {% d} months: {% d }", $ I, $ fab [$ I])."
    ";}

    8. datetime

    phpfunction getCurMonthFirstDay($date){    return date('Y-m-01', strtotime($date));} getCurMonthLastDay('2015-07-23')function getCurMonthLastDay($date){    return date('Y-m-d', strtotime(date('Y-m-01', strtotime($date)) . ' +1 month -1 day'));}

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.