phpにおけるforeach()関数とArray配列の古典的な例の説明

6321 ワード

 1 //php foreach()   Array        

 2 function getVal($v)

 3 { return  $v; //         ,    $v     ,         。}

 4 //           ,             ,        ,     ,  ,    。                   。

 5 //            foreach       

 6 //$_GET  $_POST $_COOKIE     ,PHP   , PHP   ,                           。

 7 foreach(Array('_GET','_POST','_COOKIE') as $_request)

 8 {

 9    foreach($$_request as $_k => $_v)

10    { ${$_k} = getVal($_v); }

11 }

12 //foreach()     :

13 //1: foreach(array_name as $value){  statement;     }   array_name         ,     ,array_name            $value,              ,               。

14 //2:foreach(array_name as $key => $value)    {  statement;   }                  $key,              $value ,                    $key。 

15 //Array          

16 //$person  =  Array("name"=>"  ","age"=>'20');           

17 //$person['name']="  "; $person['age']=20;

18 // ${$_k}  === $$_k;               $_k,  ¥$$_k  。

19 //       $_request       ,              _GET,_POST,_COOKIE

20 //$$_request;       $_GET , $_POST, $_COOKIE          

21 //getVal      

22 //              

23 //  foreach     

24 foreach($_GET as $_k => $_v)

25    { $$_k = $_v; } //    getVal    

26 //  foreach     

27 foreach($_POST as $_k => $_v)

28    { $$_k = $_v; } //    getVal    

29 //  foreach        

30 foreach($_COOKIE as $_k => $_v)

31    { $$_k = $_v; } //    getVal    

32 

33 //  post        $_POST['age'] = "28 "

34 foreach($_POST as $_k => $_v)  // $_k     age , $_v      28 

35    { $$_k = $_v; }  //====== {$age = '28 '}

36 

37 //                              ,                     。