遍历修改数据类型

在做APP接口的时候,返回的数据需要全部是string类型,在返回数据之前,可以使用这个函数

/**  int类型转换成string类型
 * @param $arr
 * @return array|string
 */
function intToStr($arr)
{
    if (!is_array($arr)) {
        return (string)$arr;
    }
    foreach ($arr as $key => $value) {
        if (is_array($value)){
            $arr[$key] = intToStr($value);
        }else{
            $arr[$key] = (string)$value;
        }

    }
    return $arr;
}
  • 将数组写入文件
function get_team_arr()
{
    global $le_db1;
    $sql = "select * from sb_team_names";
    $stmt = $le_db1->query($sql);
    $res = $stmt->fetchAll();
    $team_arr = array();
    foreach ($res as $array){
        $team_arr[$array['team_id']] = $array['team_name'];
    }

    $url = './team_arr.php';
    $str = '
  • 数组和独享互转函数
 public function arrayToObject($e)
    {

        if (gettype($e) != 'array') return;
        foreach ($e as $k => $v) {
            if (gettype($v) == 'array' || getType($v) == 'object')
                $e[$k] = (object)$this->arrayToObject($v);
        }
        return (object)$e;
    }

    public function objectToArray($e)
    {
        $e = (array)$e;
        foreach ($e as $k => $v) {
            if (gettype($v) == 'resource') return;
            if (gettype($v) == 'object' || gettype($v) == 'array')
                $e[$k] = (array)$this->objectToArray($v);
        }
        return $e;
    }
}

你可能感兴趣的:(遍历修改数据类型)