亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

搜索
博主信息
博文 98
粉絲 1
評論 0
訪問量 82891
最新下載
更多>
網(wǎng)站特效
網(wǎng)站源碼
網(wǎng)站素材
前端模板
常用數(shù)組函數(shù)學習
阿杰
原創(chuàng)
1329人瀏覽過

1、數(shù)組遍歷函數(shù)

(1)快速遍歷foreach

  1. $user3 = ['id'=>1,'name'=>'Jack','age'=>28,'address'=>'深圳龍華'];
  2. foreach($user3 as $key=>$value){
  3. printf('[%s]=>%s<hr>',$key,$value);
  4. }

(2) 結(jié)構(gòu)遍歷foreach

  1. // 解構(gòu)遍歷通常用來遍歷二維或者以上的數(shù)組
  2. $user4 = [
  3. ['id'=>1,'name'=>'小明'],
  4. ['id'=>2,'name'=>'張三'],
  5. ['id'=>3,'name'=>'李四'],
  6. ];
  7. foreach($user4 as list('id'=>$id,'name'=>$name)){
  8. printf('$id=%s,$name=%s<hr>',$id,$name);
  9. }

2、數(shù)組統(tǒng)計函數(shù)

(1)相加 array_sum

  1. // 相加
  2. function sum(...$args)
  3. {
  4. return array_sum($args);
  5. }
  6. echo sum(1,2,3,4,5,6,7).'<hr>';

(2)相乘 array_product

  1. // 相乘
  2. function mul(...$args)
  3. {
  4. return array_product($args);
  5. }
  6. echo '相乘積為:'.mul(2,3,4,5).'<hr>';

3、數(shù)組查詢、刪除、更新、添加

(1)數(shù)組查詢 array_slice

  1. $user = ['id'=>1,'name'=>'張三','age'=>18,'course'=>'php','grade'=>90];
  2. printf('<pre>%s<hr></pre>',print_r($user,true));
  3. // array_slice
  4. // 前兩個
  5. $res = array_slice($user,0,2);
  6. printf('<pre>%s<hr></pre>',print_r($res,true));
  7. // 后兩個
  8. $res2 = array_slice($user,-2,2);
  9. // $res2 = array_slice($user,-2,1);
  10. printf('<pre>%s<hr></pre>',print_r($res2,true));

(2)數(shù)組刪除 array_splice

  1. // array_splice
  2. $arr = [1,2,3,4,5,6,7,8,9,10];
  3. printf('<pre>%s<hr></pre>',print_r($arr,true));
  4. // 刪除:第2個位置刪除2個
  5. $res3 = array_splice($arr,1,2);
  6. printf('<pre>%s<hr></pre>',print_r($res3,true));
  7. printf('<pre>%s<hr></pre>',print_r($arr,true));

(3)數(shù)組更新

  1. // 更新:第2個位置刪除2個,使用新的數(shù)據(jù)來替換掉它
  2. $res4 = array_splice($arr,1,2,['A','B']);
  3. printf('<pre>%s<hr></pre>',print_r($res4,true));
  4. printf('<pre>%s<hr></pre>',print_r($arr,true));

(4)數(shù)組添加

  1. // 添加: 第2個位置刪除0個,傳入的新數(shù)據(jù)會追加到當前位置的后面
  2. $res5 = array_splice($arr,1,0,['hello','world']);
  3. printf('<pre>%s<hr></pre>',print_r($res5,true));
  4. printf('<pre>%s<hr></pre>',print_r($arr,true));

4、數(shù)組回調(diào)函數(shù)

(1)過濾器 array_filter

  1. // array_filter: 僅返回數(shù)組中可轉(zhuǎn)為true的元素集合
  2. $arr = [
  3. 150,
  4. 'php',
  5. true,
  6. [4, 5, 6],
  7. (new class
  8. {
  9. }),
  10. [],
  11. null,
  12. false,
  13. '',
  14. 0,
  15. '0'
  16. ];
  17. $res = array_filter($arr,function($item){
  18. if($item){
  19. // 檢測變量是否是一個標量描述
  20. return is_scalar($item);
  21. }
  22. });
  23. printf('<pre>%s<hr></pre>',print_r($res,true));

(2)過濾器 array_map

  1. // array_map
  2. $arr2 = ['php', [3, 4, 5], (new class
  3. {
  4. public $name = '電腦';
  5. public $price = 8888;
  6. }), 15, 20];
  7. printf('<pre>%s<hr></pre>',print_r($arr2,true));
  8. $res2 = array_map(function($item){
  9. switch(gettype($item)){
  10. case 'array':
  11. $item = join(',',$item);
  12. break;
  13. case 'object':
  14. $item = join(',',get_object_vars($item));
  15. }
  16. return $item;
  17. },$arr2);
  18. printf('<pre>%s<hr></pre>',print_r($res2,true));

(3)歸并 array_reduce

  1. // 3.歸并
  2. $arr3 = [10,20,30,40,50];
  3. $res3 = array_reduce($arr3,function($acc,$cur){
  4. echo $acc, ', ', $cur, '<br>';
  5. return $acc + $cur;
  6. },0);
  7. echo $res3.'<hr>';

(4)array_walk

  1. // 4.array_walk
  2. $user = ['id' => 10, 'name' => 'admin', 'email' => 'admin@php.cn'];
  3. array_walk($user,function($value,$key,$color){
  4. printf('[%s]=><span style="color:%s">%s</span>', $key, $color, $value);
  5. },'orange');

批改老師:PHPzPHPz

批改狀態(tài):合格

老師批語:
本博文版權(quán)歸博主所有,轉(zhuǎn)載請注明地址!如有侵權(quán)、違法,請聯(lián)系admin@php.cn舉報處理!
全部評論 文明上網(wǎng)理性發(fā)言,請遵守新聞評論服務協(xié)議
0條評論
作者最新博文
關(guān)于我們 免責申明 意見反饋 講師合作 廣告合作 最新更新
php中文網(wǎng):公益在線php培訓,幫助PHP學習者快速成長!
關(guān)注服務號 技術(shù)交流群
PHP中文網(wǎng)訂閱號
每天精選資源文章推送
PHP中文網(wǎng)APP
隨時隨地碎片化學習
PHP中文網(wǎng)抖音號
發(fā)現(xiàn)有趣的

Copyright 2014-2025 http://ipnx.cn/ All Rights Reserved | php.cn | 湘ICP備2023035733號

  • 登錄PHP中文網(wǎng),和優(yōu)秀的人一起學習!
    全站2000+教程免費學