dousong5492 2014-02-25 07:42 采纳率: 100%
浏览 45
已采纳

如何更改数组PHP OOP中的序列

I need your help, I trying write script in oop php to change and set current languages. At this moment I have a part for changing lang and to get your IP check where you are. Now I need a part to set current lang and this is my nightmare. This is my first step in OOP and I don't have any idea how I can do that. This is my script:

<?php
class IP{
  static $fields = 24578; //65535;     // refer to http://ip-api.com/docs/api:returned_values#field_generator
  static $use_xcache = false;  // set this to false unless you have XCache installed (http://xcache.lighttpd.net/)
  static $api = "http://ip-api.com/php/";

  public $status, $country, $countryCode, $region, $regionName, $city, $zip, $lat, $lon, $timezone, $isp, $org, $as, $reverse, $query, $message;

  public static function query($q){
    $data = self::communicate($q);
    $result = new self;
    foreach($data as $key => $val){
      $result->$key = $val;
    }
    return $result;
  }

  private static function communicate($q){
    $q_hash = md5('ipapi'.$q);
    if(self::$use_xcache && xcache_isset($q_hash)){
      return xcache_get($q_hash);
    }
    if(is_callable('curl_init')){
      $c = curl_init();
      curl_setopt($c, CURLOPT_URL, self::$api.$q.'?fields='.self::$fields);
      curl_setopt($c, CURLOPT_HEADER, false);
      curl_setopt($c, CURLOPT_TIMEOUT, 30);
      curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
      $result_array = unserialize(curl_exec($c));
      curl_close($c);
    } else{
      $result_array = unserialize(file_get_contents(self::$api.$q.'?fields='.self::$fields));
    }
    if(self::$use_xcache){
      xcache_set($q_hash, $result_array, 86400);
    }
    return $result_array;
  }

  public $client_ip;
  public function getIP(){ 
    return "173.194.78.94";
    /*
    if (getenv("HTTP_CLIENT_IP")) $this->client_ip = getenv("HTTP_CLIENT_IP"); 
    else if(getenv("HTTP_X_FORWARDED_FOR")) $this->client_ip = getenv("HTTP_X_FORWARDED_FOR"); 
    else if(getenv("REMOTE_ADDR")) $this->client_ip = getenv("REMOTE_ADDR"); 
    else $this->ip = "UNKNOWN";
    return $this->client_ip; 
    */
  }
}

class Lang{
  private $languages = array('pl' => 'Polish', 'en' => 'English', 'ru' => 'Russian');

  public function showIP(){
    $query = IP::query('');
    echo 'Country code: '.$query->countryCode.' your ip: '.$query->query;

    if($query->countryCode == 'PL'){
      $this->languages = array('pl' => 'Polish', 'en' => 'English', 'ru' => 'Russian');
      return $this->languages;
    }
    elseif($query->countryCode == 'RU' || $query->countryCode == 'BY' || $query->countryCode == 'KZ' || $query->countryCode == 'KG'){
      $this->languages = array('ru' => 'Russian', 'en' => 'English', 'pl' => 'Polish');
      return $this->languages;
    }
    else{
      $this->languages = array('en' => 'English', 'ru' => 'Russian', 'pl' => 'Polish');
      return $this->languages;
    }

  }

  private $current_language = false;
  private $lines = array();
  private static $instance = false;

  public function __construct(){
    $this->set_language();
  }

  public static function instance(){
    if(self::$instance == FALSE){self::$instance = new Lang;}
    return self::$instance;
  }

  private function set_language(){
    if(isset($_GET['lang']) AND array_key_exists(($lang = strtolower($_GET['lang'])), $this->languages)){
      $this->current_language = $lang;
    }
    else{
      $this->current_language = $this->default_language();
    }
    $this->load_lang_file();
  }

  private function load_lang_file(){
    if(file_exists('langs/'.$this->current_language.'.php')){
      include 'langs/'.$this->current_language.'.php';
      $this->lines = $lang;
    }
  }

  private function default_language(){
    return current(array_keys($this->languages));
  }

  public static function line($name = FALSE, $params = array()){
    if(isset(self::instance()->lines[$name]))
      return vsprintf(self::instance()->lines[$name], $params);

    return FALSE;
  }

  public static function get_language(){
    return self::instance()->current_language;
  }

  public static function get_languages(){
    return self::instance()->languages;
  }
}

function __($name = FALSE, $params = array()){
  return Lang::line($name, $params);
}
?>

I have a problem with this part:

private $languages = array('pl' => 'Polish', 'en' => 'English', 'ru' => 'Russian');

public function showIP(){
  $query = IP::query('');
  echo 'Country code: '.$query->countryCode.' your ip: '.$query->query;

  if($query->countryCode == 'PL'){
    $this->languages = array('pl' => 'Polish', 'en' => 'English', 'ru' => 'Russian');
    return $this->languages;
  }
  elseif($query->countryCode == 'RU' || $query->countryCode == 'BY' || $query->countryCode == 'KZ' || $query->countryCode == 'KG'){
    $this->languages = array('ru' => 'Russian', 'en' => 'English', 'pl' => 'Polish');
    return $this->languages;
  }
  else{
    $this->languages = array('en' => 'English', 'ru' => 'Russian', 'pl' => 'Polish');
    return $this->languages;
  }
}

I try change keys in array, example now this working like that: if first element in array is english so you current lang is english, if first element is polish so your current lang in this website is polish. I try change that and add geolocalization, but I dont have idea how I can change sequence in array or maybe is a simpler way.

  • 写回答

1条回答 默认 最新

  • douwei1904 2014-02-25 08:07
    关注

    If you just want to "resort" the keys in your array - which I believe is your actual question, then you have to extract the key you want from the array and re-add it to the beginning of the list with array_unshift. You cannot 'sort' them in any classic way, as you're looking for a very specific order, not a standard alphabetical (or other sortable) method. You're prioritising associated keys.

    Easiest way to do this is:

    $lang = array('pl' => 'Polish', 'en' => 'English', 'ru' => 'Russian');
    $selectedLang = $lang['en'];
    unset($lang['en']);
    array_unshift($lang,$selectedLang);
    var_dump($lang);
    

    If you want to apply that to your code example (I would change the if to a switch):

    $this->languages = array('pl' => 'Polish', 'en' => 'English', 'ru' => 'Russian');
    switch($query->countryCode) {
        case 'PL':          
          $selectedLang = $this->languages['pl'];
          unset($this->languages['pl']); 
          array_unshift($this->languages,$selectedLang);
          return $this->languages;
        break;   
    
        case 'RU':
          $selectedLang = $this->languages['ru'];
          unset($this->languages['ru']); 
          array_unshift($this->languages,$selectedLang);
          return $this->languages;
        break; 
    
         default:
          $selectedLang = $this->languages['en'];
          unset($this->languages['en']); 
          array_unshift($this->languages,$selectedLang);
          return $this->languages;
        break; 
    
    }
    

    Even better would be refactoring the code in a function and using your countrycode variable as an argument

    function selectLang($langstring,$lang) {
      $selectedLang = $lang[$langstring];
      unset($lang[$langstring]);
    
      array_unshift($lang,$selectedLang);
      return $lang; 
    }
    
    $this->languages = array('pl' => 'Polish', 'en' => 'English', 'ru' => 'Russian');
    return selectLang( strtolower ($query->countryCode), $this->languages),
    

    Hope this helps

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)