改变数组的键值

1.改变多维数组的键值

/**
用法: $keyEnArray = array("奖品" , "标识","编码"); $keyZhCnArray = array('prize', 'sign','code'); $result = changeKeys($data, $keyEnArray, $keyZhCnArray);
 $keyEnArray  原键值
 $keyZhCnArray  改变后的键值
*/
function changeKeys($array, $keyEnArray, $keyZhCnArray)
{
    if(!is_array($array)) return $array;
    $tempArray = array();
    foreach ($array as $key => $value){
        // 处理数组的键,翻译成中文
        $key = array_search($key, $keyEnArray, true) === false ? $key : $keyZhCnArray[array_search($key, $keyEnArray)];
        if(is_array($value)){
            $value = changeKeys($value, $keyEnArray, $keyZhCnArray);
        }
        $tempArray[$key] = $value;
    }
    return $tempArray;
}

2.循环三维数组,插入数据库

 foreach($result as $value){
     foreach($value as $key=> $v){
          $arr2[$key]=$v;
     }
    pdo_insert('hxqlottery_prize',$arr2);
}        

 

posted @ 2018-06-13 09:24  Tane  阅读(195)  评论(0编辑  收藏  举报