php 获取二维数组中某一字段值的最大最小值

这里用到的是TP3.2框架

数据库字段截图 要获取step的最大值
php 获取二维数组中某一字段值的最大最小值_第1张图片
后台代码:

//根据条件 查出来的二维数组
 $ymtbsh = $ymtbsh_model->where(['tbid' => $id])->order("step asc")->select();
//我要获取$ymtbsh中step的最大值和最小值

        $max['key'] = '';
        $max['value'] = '';
        $min['key'] = '';
        $min['value'] = '';

        foreach ($ymtbsh as $key => $val) {

            if($max['key'] === ''){

                $max['key'] = $key;
                $max['value'] = $val['step'];

            } 
            
            if((int)$max['value'] < (int)$val['step']){

                $max['key'] = $key;
                $max['value'] = $val['step'];

            } 

            if($min['key'] === ''){

                $min['key'] = $key;
                $min['value'] = $val['step'];

            } 
            
            if( (int)$min['value'] > (int)$val['step']){

                $min['key'] = $key; 
                $min['value'] = $val['step'];
            } 

        }
        var_dump($max);
        var_dump($min);die;
	打印结果:   array (size=2)
				  'key' => int 2
				  'value' => string '3' (length=1)
				array (size=2)
				  'key' => int 0
				  'value' => string '1' (length=1)

你可能感兴趣的:(PHP)