<?php
class data_change{
private $data = [];
private $rolue = ["L","getkey","getvalue","",""];
private $rolue_v = '';
private $key = [];
private $value = [];
private $first_line = false;
private $p_key = 'id';
public $res_data = [];
public function __construct($data, $rolue = 0 , $p_key = 'id', $first_line = false){
if (!$data)
return false;
$this->data = $data;
$this ->p_key = $p_key;
$this->rolue_v = $this->rolue[$rolue];
$this->first_line = $first_line;
$this ->run();
}
private function run(){
if($this->rolue_v == "L"){
$this->getkeys($this->data);
if(in_array($this->p_key ,$this->key)){
$this->data = $this->sort_array2($this->data,$this->p_key);
}
// 可以是id 值段 也可以是 bool true false 等字符串 (true 去重)
$this->getvalues($this->data,$this->p_key);
// 获取到主键的编号 随后添加一项
$max_id = max($this->value);
$new_arr = [$this->p_key=>$max_id+1];
array_push($this->data,$new_arr);
array_push($this->value,$max_id+1);
$this->L_change();
}else if($this->rolue_v == "getkey"){
$this->getkeys($this->data);
$this->res_data = $this->key;
}else if($this->rolue_v == "getvalue"){
$this->getvalues($this->data,$this->p_key);
$this->res_data = $this->value;
}
}
private function L_change(){
$length = count($this->value);
foreach($this->data as $kd => $vd){
if(0 < $kd && $kd <= $length -1){
if($kd == $length -1){
$this->data[$kd] =$this->data[$kd-1];
}
//保持最后一个值不变
$this->data[$kd][$this->p_key] = $this->value[$kd-1];
}
}
// 得到结果是 0 1 位置 id是一样的 如果 保持首相不变那么去除 [1] 如果首相需需要变化那么去除 [0]
// 最后一个值空也就是说在创建钱先创建一个空的数组放在最底下那么 第一个初始化时添加即可
// 第一项 是否保留
if($this->first_line == false){
unset($this->data[0]);
$this->data = array_values($this->data);
}else{
unset($this->data[1]);
$this->data = array_values($this->data);
}
$this ->res_data = $this->data;
}
// 获取键值
public function getkeys($arr){
if(!$arr){
return false;
}
$new_key = [];
$this->key = $this ->foreach_dg($arr , $new_key , 'k');
}
// 获取value
public function getvalues($arr,$str){
if(!$arr){
return false;
}
$new_value = [];
// 是否去重 :v_true :v_false ,获取对应字段的全部value
$str = 'v_'.$str;
$this->value = $this ->foreach_dg($arr , $new_value , $str);
}
private function foreach_dg($arr , &$res_arr, $type){
foreach ($arr as $k => $va){
if($type == 'k'){
if(strlen($k) !== strlen(intval($k))){
if(!in_array($k,$res_arr)){
array_push($res_arr , $k);
}
}else{
if(is_array($va)){
$this->foreach_dg($va , $res_arr ,$type);
}
continue;
}
}else{
if(is_array($va)){
$this->foreach_dg($va , $res_arr ,$type);
}else{
if($type == 'v_true'){
if(!in_array($va,$res_arr)){
array_push($res_arr , $va);
}
}else if($type == 'v_false'){
array_push($res_arr , $va);
}else{
$need_key = explode("_",$type)[1];
if($need_key == $k){
array_push($res_arr , $va);
}
continue;
}
}
}
}
return $res_arr;
}
// 二维数组根据某个key 排序
function sort_array2($arr,$key,$orderby= SORT_ASC){
// SORT_DESC SORT_ASC
array_multisort(array_column($arr,$key),$orderby,$arr);
return $arr;
}
}
$arr = [
array(
"id" => '12',
"yttt" => 'hello',
'lkyy' => 'pppl'
),
array(
"id" => '13',
"yttt" => 'hello2',
'lkyy' => 'pppl1'
),
array(
"id" => '15',
"yttt" => 'hello3',
'lkyy' => 'pppl2'
),
array(
"id" => '17',
"yttt" => 'hello33',
'lkyy' => 'pppl21'
),
array(
"id" => '25',
"yttt" => 'hello31',
'lkyy' => 'pppl22'
),
array(
"id" => '29',
"yttt" => 'hello13',
'lkyy' => 'pppl12'
)
];
$a = new data_change($arr,2,'yttt');
var_dump($a->res_data);die;
算法(多维数组获取所有key,value值(不包含数字key) ,还能让数组内项 的值替换(后一项的值替换前一项)(算法))
最新推荐文章于 2025-10-31 11:14:13 发布
这段代码定义了一个PHP类`data_change`,用于处理二维数组。类中有不同的方法来获取键值和值,以及根据指定键进行排序。主要功能包括按主键排序、在数据末尾添加新记录、根据特定字段获取唯一值等。示例中,类被用于处理一个包含多个ID、yttt和lkyy字段的数组,并输出处理后的结果。
4830

被折叠的 条评论
为什么被折叠?



