PHP问题与数组到字符串转换

时间:2017-10-25 13:56:21

标签: php arrays

今天我尝试将PHP版本从5.6更改为7.0。 而且大部分代码都能正常工作,但是我遇到了数组问题。

CODE:

$settings = Core_Settings::find_by_sql("SELECT * FROM core_settings");
foreach($settings as $setting) 
{
    $array =  (array) $setting;
    $$array['name'] = $array['data'];
}

$settings = 
Array ( [0] => Core_Settings Object ( [name] => WWW [data] => http://10.20.31.230/ ) [1] => Core_Settings Object ( [name] => SITE_NAME [data] => Istracom - VPN Access Manager ) [2] => Core_Settings Object ( [name] => SITE_DESC [data] => UPRAVLJAČKI SUSTAV ZA VPN VEZE! ) [3] => Core_Settings Object ( [name] => SITE_KEYW [data] => vpn ) [4] => Core_Settings Object ( [name] => ADMINDIR [data] => admin/ ) [5] => Core_Settings Object ( [name] => SITE_EMAIL [data] => Istracom d.o.o. ) [6] => Core_Settings Object ( [name] => DATABASE_SALT [data] => 293847 ) [7] => Core_Settings Object ( [name] => PAGINATION_PER_PAGE [data] => 20 ) [8] => Core_Settings Object ( [name] => TIMEZONE [data] => Europe/Sarajevo ) [9] => Core_Settings Object ( [name] => ADMIN_LEVEL [data] => 293847 ) [10] => Core_Settings Object ( [name] => THEME_NAME [data] => istracom ) [11] => Core_Settings Object ( [name] => BRUTEFORCE_LIMIT [data] => 5 ) [12] => Core_Settings Object ( [name] => BRUTEFORCE_TIMEOUT [data] => 0 ) [13] => Core_Settings Object ( [name] => LOGO [data] => https://vpn.jadranka.hr/vpn/assets/img/jadlogo.png ) [14] => Core_Settings Object ( [name] => ADMIN_EMAIL [data] => itpodrska@jadranka.hr ) [15] => Core_Settings Object ( [name] => VERIFY_EMAIL [data] => YES ) [16] => Core_Settings Object ( [name] => PRAVILA [data] => https://vpn.jadranka.hr/vpn/downloads/VPN%20-%20korisni%C4%8Dka%20pravila.pdf ) [17] => Core_Settings Object ( [name] => PRAVILAUSERA [data] => VPN korisničko ime ne možete naknadno promijeniti. Korisničko ime VPN-a se koristi za ostvarivanje VPN veza. VPN korisničko ime jednako je unesenoj email adresi. ) [18] => Core_Settings Object ( [name] => PRAVILAUSERNAME [data] => Korisničko ime (username) koristi za povezivanje na WEB sustav koji Vam omogućava funkcije kreiranja VPN zahtjeva i ostale sistemske postavke. WEB korisničko ime ne možete naknadno promijeniti. Korisničko ime jednako je unesenoj email adresi. ) [19] => Core_Settings Object ( [name] => PRAVILALOZINKA [data] => Lozinka mora sadržavati znakove iz tri kategorije (malo slovo, veliko slovo, znamenka). Duljina lozinke mora biti minimalno 6 znakova. ) [20] => Core_Settings Object ( [name] => OBAVJESTI [data] => 0 ) [21] => Core_Settings Object ( [name] => USER_PASSWORD [data] => VPNManPassword ) [22] => Core_Settings Object ( [name] => VPN_OBAVJESTI [data] => danijela.bandalo@jadranka.hr ) [23] => Core_Settings Object ( [name] => ODRZAVANJE_SUSTAVA [data] => NO ) ) 

错误日志:

  

数组到字符串转换

     

未定义的变量:XXX

1 个答案:

答案 0 :(得分:0)

带数组的双元符号会产生问题,因为$ array变量本身就是一个数组。因此,分别解析完整的$ array [' name']。使用此解决方案:

foreach($settings as $setting)
{  
    $array =  (array) $setting;
    ${$array['name']} = $array['data'];
}

OR

foreach($settings as $setting)
{  
    $array =  (array) $setting;
    $name = $array['name'];    
    $$name = $array['data'];
}