• PHP 数组 函数实例


    $arr =explode(",",字符串); //explode字符串转换成数组  
    空格字符窜是空格也可以转成数组
    $arr =implode(",",数组); //implode数组转换成字符
    
    //============返回数组中指定的一列=============================
    array_column()   //array_column获取数组里面的某个字段转成一位维数组
    实例:
    $info= “select * from sb_htwy_articles where status=1 and picpath!='' ORDER BY createtime DESC limit 3”;   //只查询最新三条  //查询二维数组
    $id = implode(',',array_column($info,'id'));    // 获取前三条id
    //=======================================================
    
    //=============计算二维数组里面的某个值总和===================
    $text = array(
        array("dosage"=>5,"content"=>2),
        array("dosage"=>2,"content"=>2),
        array("dosage"=>2,"content"=>2),
    );
    $arr1 = array_column($text, 'dosage');  //一维数组,拿到自己想要计算的数组值
    $total = array_sum($arr1);
    print_r($total);die;
    //====================================================
    
    
    //===============判断$_POST['school_id']数组里面是否有重复的值======================
    count($_POST['school_id']) != count(array_unique($_POST['school_id']))
    $intersection = array_diff($arr,$school_ids); //取两个数组的差集 $arr是个数组和$school_ids另一个数组
    //============================================================================
    
    //============array_filter函数 将字串转成数组并过滤其中的空值=========================
    $text = Array
    (
        [0] =>
        [1] =>
        [2] =>
        [3] => 11
        [4] => 8, 9, 10
        [5] => 5, 7
        [6] => 4
        [7] => 2, 3, 12
        [8] =>
        [9] =>
    )
    $a = implode(",", $text)
    print_r($a);
    //打印出来为
    $b=,,,118,9,10,5,7,4,2,3,12,,
    $c =  array_filter(implode(",", $text)));
    //=====================================================================================
    //数组进行排序
    $sort = array_column($nodes,'sort');
    array_multisort($sort,SORT_ASC,$nodes);


  • 相关阅读:
    在Eclipse/STS里添加代码反编译器(.class)步骤
    关于页面添加字段
    关于jeesite Date和时间戳问题
    关于前端ajax通过实体类向后端传参报不存在问题
    引入字典
    关于添加主键
    简单的前后端分离项目 部署到 centos7
    Cenost7 Mysql5.7 安装 并打开远程访问
    npm install vue-cli -g 报错
    docker 笔记 docker 基础 docker 常用命令
  • 原文地址:https://www.cnblogs.com/xiangangXu1997/p/12522502.html
Copyright © 2020-2023  润新知