thesnarkie Posted September 22, 2010 Report Share Posted September 22, 2010 Partneris, izsaucot linku viņa pusē, padod šādu atbildi: id=1 name=Jānis gender=male age=20 friends=0 Kā lai efektīvāk tādu lasa un pārtaisa sakarīgā array? Quote Link to comment Share on other sites More sharing options...
briedis Posted September 22, 2010 Report Share Posted September 22, 2010 (edited) Sūdīgs partneris, vismaz XML vai JSON varēja padot... Iespējamais risinājums - foreach cikls ar explode divām funkcijām, easy peasy. EDIT: Diemžēl Лешийs jau priekšā uzrakstīja, nemaz nedodot iespēju OP pakustināt smadzenes un izdomāt pašam :) Es šeit esmu ar uzskatu, ka jādod makšķere, nevis zivis :p Edited September 22, 2010 by briedis Quote Link to comment Share on other sites More sharing options...
Леший Posted September 22, 2010 Report Share Posted September 22, 2010 (edited) ja zini 100%, ka separātors vienmēr ir atstarpe, tad $array = explode(' ', $response); $newArray = array(); foreach ($array as $value){ $val = explode('=', $value); $newArray[$val[0]] = $val[1]; } Edited September 22, 2010 by Леший Quote Link to comment Share on other sites More sharing options...
thesnarkie Posted September 22, 2010 Author Report Share Posted September 22, 2010 Briedi, paldies par ideju un Лешийs par piemēru. :) Quote Link to comment Share on other sites More sharing options...
v3rb0 Posted September 22, 2010 Report Share Posted September 22, 2010 (edited) bet ja name ir 'Jānis Kociņš' vai '-==JanizZ==-'? Edited September 22, 2010 by v3rb0 Quote Link to comment Share on other sites More sharing options...
thesnarkie Posted September 22, 2010 Author Report Share Posted September 22, 2010 Tur name būs tikai lietotājvārds, kas var sastāvēt tikai no a-z,A-Z,0-9. Quote Link to comment Share on other sites More sharing options...
Леший Posted September 22, 2010 Report Share Posted September 22, 2010 Es šeit esmu ar uzskatu, ka jādod makšķere, nevis zivis :p Viņš vēl paspiningos, kad klients tomēr sūtīs datus ar atstarpēm ;) Quote Link to comment Share on other sites More sharing options...
v3rb0 Posted September 22, 2010 Report Share Posted September 22, 2010 (edited) ja dati/key ir tikai a-z,A-Z,0-9 un nav vairāk par vienu atstarpi pēc kārtas, tad šo var īsi arī ar eval() un str_replace() :> $s='id=1 name=Jānis gender=male age=20 friends=0'; eval('return array("'.str_replace(array(' ', '='),array('","', '"=>"'), $s).'");'); Edited September 22, 2010 by v3rb0 Quote Link to comment Share on other sites More sharing options...
briedis Posted September 22, 2010 Report Share Posted September 22, 2010 (edited) eval == evil Nu kā var taisīt eval datiem kas nāk sazin no kurienes? :) Edited September 22, 2010 by briedis Quote Link to comment Share on other sites More sharing options...
Леший Posted September 22, 2010 Report Share Posted September 22, 2010 Vēl nav garantijas, ka eval normāli darbosies ar UTF-8. Informāciju neatradu. Quote Link to comment Share on other sites More sharing options...
indoom Posted September 22, 2010 Report Share Posted September 22, 2010 Var arī šitā $atbilde = 'id=1 name=Jānis gender=male age=20 friends=0'; $arrays = array(); parse_str(str_replace(' ','&',$atbilde), $arrays); Quote Link to comment Share on other sites More sharing options...
thesnarkie Posted September 22, 2010 Author Report Share Posted September 22, 2010 Var arī šitā $atbilde = 'id=1 name=Jānis gender=male age=20 friends=0'; $arrays = array(); parse_str(str_replace(' ','&',$atbilde), $arrays); Šis variants manuprāt ir ideāls, jo darbojas arī tad, ja kāds no value satur =. Quote Link to comment Share on other sites More sharing options...
indoom Posted September 22, 2010 Report Share Posted September 22, 2010 ja valuē būs & vai +, tad tie jānokodē (vai arī + paliks par atstarpi un & izveidos jaunu keyu) Atstarpes valuēs šajā gadījumā nevar izmantot. parse_str(str_replace(array('&','+',' '),array('%26','%2B','&'),$atbilde), $arrays); Quote Link to comment Share on other sites More sharing options...
Kemito Posted September 22, 2010 Report Share Posted September 22, 2010 KO darīt ja mums vecais labais jūzeris padod vienkāršas atstarpes pa gaisu vai arī " šitaste" ? Notrimmot tā teikt vai tad nevaig? ( trim() ) ? Quote Link to comment Share on other sites More sharing options...
Леший Posted September 22, 2010 Report Share Posted September 22, 2010 Ja tomēr būs atstarpes, tad ir jāmeklē "{dajebkas}{atstarpe}vārds_bez_atstarpes=" un vārds_bez_atstarpes būs mainīga nosaukums, bet {dajebkas} būs iepriekšeja mainīga vērtība. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.