Home> headlines> body text

php array traversal skills

无忌哥哥
Release: 2018-06-28 10:31:21
Original
1984 people have browsed it

* Array traversal

* 1. foreach($arr as $key => $value) {}

* 2. Principle: Array $arr according to key value pair in sequence Take it out into $key=>$value and process it one by one, similar to callback

* 3. If you only process the value, you can omit $key: foreach($arr as $value) {}

* 4. Especially suitable for associative arrays, of course also suitable for index arrays

$teacher = ['id'=>1001, 'name'=>'peter zhu', 'salary'=> 3000, 'course'=>'php'];
Copy after login

//Use for loop to implement traversal of associative array

for ($i=0; $i',current($teacher),'
'; next($teacher); } echo '
';
Copy after login

//Use while loop to implement

reset($teacher); $i = 0; while ($i',current($teacher),'
'; next($teacher); $i++; } echo '
'; //foreach($arr as $key=>$value):数组专用的遍历语法结构 echo '

讲师信息

'; echo '
    '; foreach ($teacher as $key => $value) { echo '
  • '.$key.':'.$value.'
  • '; } echo '
'; echo '
'; //如果只对值感兴趣 echo ''; echo ''; echo ''; echo ''; foreach ($teacher as $value) { echo ''; } echo ''; echo '
讲师信息表
ID姓名工资课程
'.$value.'
'; echo '
';
Copy after login

//Create a date picker

echo '

Select your birthday:

';

//Generate year

$years = range(1980, 2000); echo ''; echo ' ';
Copy after login

//Generation month

$months = range(1, 12); echo ''; echo ' ';
Copy after login

//Generation day

$days = range(1, 31); echo ''; echo ' '; echo '';
Copy after login
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!