登录
首页 >  数据库 >  MySQL

PHP基础(一)数组处理函数

来源:SegmentFault

时间:2023-01-21 11:58:51 153浏览 收藏

积累知识,胜过积蓄金银!毕竟在##column_title##开发的过程中,会遇到各种各样的问题,往往都是一些细节知识点还没有掌握好而导致的,因此基础知识点的积累是很重要的。下面本文《PHP基础(一)数组处理函数》,就带大家讲解一下MySQL、面试、Linux、PHP、lavarel知识点,若是你对本文感兴趣,或者是想搞懂其中某个知识点,就请你继续往下看吧~

  • $a = array(
            array('name' => 'zhangsan', 'age'=>12),
            array('name' => 'lisi', 'age'=>13),
            array('name' => 'wangwu', 'age'=>14)
        );
        
    $b = array_column($a, 'name');
    print_r($b);//Array ( [0] => zhangsan [1] => lisi [2] => wangwu )
    
    $c = array_column($a, 'age', 'name');
    print_r($c);//Array ( [zhangsan] => 12 [lisi] => 13 [wangwu] => 14 )
    • $keys = array("zhangsan","lisi","wangwu");
      $values = array(11,12,13);
      
      $c = array_combine($keys, $values);
      print_r($c);//Array ( [zhangsan] => 11 [lisi] => 12 [wangwu] => 13 )
      • $a = ['zhangsan',1,'lisi','lisi','a'];
        $b = array_count_values($a);
        print_r($b);//Array ( [zhangsan] => 1 [1] => 1 [lisi] => 2 [a] => 1 )
        • $a = array_fill(-1,5,'a');
          print_r($a);//Array ( [-1] => a [0] => a [1] => a [2] => a [3] => a )
          • $a = [1,2,3,4,10,11,23];
            $b = array_filter($a,function($val){
                return $val > 5 ? true : false;
            });
            print_r($b);//Array ( [4] => 10 [5] => 11 [6] => 23 )
            • $a = array('zhangsan'=>'shanxi','lisi'=>'hubei','wangwu'=>'shanxi','zhaoliu'=>'shanghai');
              $b = array_flip($a);
              print_r($b);//Array ( [shanxi] => wangwu [hubei] => lisi [shanghai] => zhaoliu )
              • $a = ['name'=>'张三','age'=>12,'sex'=>'a','b'];
                $b = ['name'=>'张三','age'=>11,'a','b'];
                $c = array_intersect_assoc($a, $b);
                print_r($c);//Array ( [name] => 张三 )
                • $a = ['name'=>'张三','age'=>12,'sex'=>'a','b'];
                  $b = ['name'=>'张三','age'=>11,'a','b'];
                  $c = array_intersect($a, $b);
                  printr_($c);//Array ( [name] => 张三 [sex] => a [0] => b )

                      含有类似功能的函数还有:

                  $a = ['name'=>'张三','age'=>12,'sex'=>'a','b'];
                  print_r(array_key_first($a));//name
                  • $a = ['name'=>'张三','age'=>12,'sex'=>'a','b'];
                    print_r(array_key_last($a));// 0
                    • $a = ['name'=>'张三','age'=>12,'sex'=>'a','b'];
                      $b = array_keys($a);
                      $c = array_keys($a, 12);
                      print_r($b);//Array ( [0] => name [1] => age [2] => sex [3] => 0 ) 
                      print_r($c);//Array ( [0] => age )
                      • $a = [1,2,3,4,5];
                        $b = array_map(function($val){
                            return $val*$val;
                        }, $a);
                        print_r($b);//Array ( [0] => 1 [1] => 4 [2] => 9 [3] => 16 [4] => 25 )
                        • $a = ['name'=>1,'age'=>2,3];
                          $b = ['age'=>4,2,'sex'=>1];
                          $c = array_merge($a,$b);
                          print_r($c);//Array ( [name] => 1 [age] => 4 [0] => 3 [1] => 2 [sex] => 1 )

                                 当两个数组相加时:

                          $a = ['name'=>1,'age'=>2,3];
                          $b = ['age'=>4,2,'sex'=>1];
                          $c = $a+$b;
                          print_r($c);//Array ( [name] => 1 [age] => 2 [0] => 3 [sex] => 1 )
                          • $a = [1,2,3];
                            $c = array_pad($a, 5, 99);
                            print_r($c);//Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 99 [4] => 99 )
                            
                            $c = array_pad($a, 2, 88);
                            print_r($c);// Array ( [0] => 1 [1] => 2 [2] => 3 )
                            • $a = [1];
                              $b = array_pop($a);
                              print_r($a);//Array ( [0] => 1 [1] => 2 )
                              print_r($b);//3
                              
                              $c = array_pop($a);
                              print_r($a);//Array ( [0] => 1 [1] => 2 )
                              print_r($b);//3
                              • $a = [1,3,5,7,9];
                                $b = array_product($a);
                                print_r($b);//945
                                
                                $c = [1,2,3,'a'];
                                $b = array_product($a);
                                print_r($b);//0
                                • $a = [1,2,3];
                                  $b = array_push($a,4,5);
                                  print_r($a);//Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 )
                                  print_r($b);//5
                                  • $a = [1,2,3,'a'=>4];
                                    $b = array_reverse($a);
                                    $c = array_reverse($a,true);
                                    print_r($b);//Array ( [a] => 4 [0] => 3 [1] => 2 [2] => 1 )
                                    print_r($c);//Array ( [a] => 4 [2] => 3 [1] => 2 [0] => 1 )
                                    • $a = ['a'=>1,'b'=>2,'c'=>1];
                                      $b = array_search(1, $a);
                                      print_r($b);// a
                                      • $a = [2,'a'=>1,3,4];
                                        $b = array_shift($a);
                                        print_r($a);// Array ( [a] => 1 [0] => 3 [1] => 4 )
                                        print_r($b);// 2
                                        • $a = [1,2,3,'a'];
                                          $b = array_sum($a);
                                          print_r($b);//6
                                          • $a = [1,2,3,4];
                                            $b = array_unshift($a, 0,9,8);
                                            print_r($a);// Array ( [0] => 0 [1] => 9 [2] => 8 [3] => 1 [4] => 2 [5] => 3 [6] => 4 )
                                            print_r($b);// 7
                                            • $a =['name' => 1,'age' => 2,'sex'=>3];
                                              $b = array_walk($a, function(&$val, $key){
                                                  $val += 1;
                                              });
                                              print_r($a);//Array ( [name] => 2 [age] => 3 [sex] => 4 ) 

                                              到这里,我们也就讲完了《PHP基础(一)数组处理函数》的内容了。个人认为,基础知识的学习和巩固,是为了更好的将其运用到项目中,欢迎关注golang学习网公众号,带你了解更多关于mysql的知识点!

声明:本文转载于:SegmentFault 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>
评论列表