Transfer function between PHP array _5_3_ array processing functions and their application _4_ arrays and variables

The following is learning Kong Xiangsheng editor of "PHP Programming Fundamentals tutorial and example" (second edition) notes made.

 

Transfer function between arrays and variables

1. list () language structure

program:

1 <?php
2 $info = array('coffee','brown','caffeine');
3 list($drink,$color,$power) = $info;
4 echo "$drink is $color and $power makes it special.<br/>";  //coffee is brown and caffeine makes it special.
5 list($drink,,$power) = $info;
6 echo "$drink has $power.<br/>";     //coffee has caffeine.
7 echo "I need $power.";      //I need caffeine.
8 ?>

Output:

coffee is brown and caffeine makes it special.
coffee has caffeine.
I need caffeine.

 

2. extract () function

program:

 

 1 <?php
 2 $info = array("studentNo"=>"2010001","studentName"=>"张三","studentSex"=>"男");
 3 extract($info);
 4 echo $studentNo;
 5 echo "<br/>";
 6 echo $studentName;
 7 echo "<br/>";
 8 echo $studentSex;
 9 echo "<br/>";
10 ?>

Output:

2010001 
Joe Smith 
Male

 

3. compact () function

program:

1 <?php
2 $tel = "135***00000";
3 $email = "[email protected]";
4 $postCode = "453700";
5 $result = compact("tel","email","postCode");
6 print_r($result);   
7 ?>

Output:

Array ( [tel] => 135***00000 [email] => [email protected] [postCode] => 453700 )

 

Iterate

Use list () structure, each () function and the loop may be implemented through the array.

program:

1 <?php
2 $colors = array('orange','red','yellow');
3 $fruits = array('orange','apple','banana');
4 $temp = array_combine($colors,$fruits);
5 reset($temp);
6 while(list($key,$value)=each($temp)){
7     echo $key."==>".$value."<br/>";
8 }
9 ?>

Output:

 

Description:

  PHP 7.2 abandoned each () method.

 

Guess you like

Origin www.cnblogs.com/xiaoxuStudy/p/11825008.html