aika Posted November 7, 2010 Report Share Posted November 7, 2010 Uzdevums salādēt stringu arajā, taču pie nosacījuma ka rindas nekārtnais elements ir araja keys, bet kārtnais - vērtība. Piemērs: balts;2;melns;3;sarkans;5 => array('balts'=>2,'melns'=>3, 'sarkans'=>5) Ja ērtāk, tad strings var būt arī: balts=2;melns=3;sarkans=5; Šķietami vienkāršais uzdevums izrādījās nemaz tik viegls nav. Quote Link to comment Share on other sites More sharing options...
briedis Posted November 7, 2010 Report Share Posted November 7, 2010 (edited) Uzdevums ir ļoti viegls. Palasi par explode() funkciju un foreach ciklu. Edited November 7, 2010 by briedis Quote Link to comment Share on other sites More sharing options...
marcis Posted November 7, 2010 Report Share Posted November 7, 2010 array_filter() ftw function odd($var){ static $c = 0; return !(++$c%2); } function even($var){ static $c = 0; return ++$c%2; } $str = 'balts;2;melns;3;sarkans;5'; $tmp = explode(';', $str); $arr = array_combine(array_filter($tmp, 'odd'), array_filter($tmp, 'even')); print_r($arr); /* Array ( [2] => balts [3] => melns [5] => sarkans ) */ Quote Link to comment Share on other sites More sharing options...
aika Posted November 7, 2010 Author Report Share Posted November 7, 2010 (edited) paldies! man gan vajag otrādāk, bet to jau nu es izlabošu! return !(++$c%2); šito lūdzu var paskaidrot!! Edited November 7, 2010 by aika Quote Link to comment Share on other sites More sharing options...
marcis Posted November 7, 2010 Report Share Posted November 7, 2010 (edited) http://lv.php.net/manual/en/language.operators.increment.php http://lv.php.net/manual/en/language.operators.arithmetic.php http://lv.php.net/manual/en/language.operators.logical.php ++ -> pieskaita +1 un atgriež jauno vērtību %2 -> atgriež dalījuma atlikumu ! -> atgriež pretējo boolean vērtību (ja dalījuma atlikums bija nulle (false), tad šis atgriež true) Edited November 7, 2010 by marcis Quote Link to comment Share on other sites More sharing options...
indoom Posted November 8, 2010 Report Share Posted November 8, 2010 (edited) $string = 'balts=2;melns=3;sarkans=5;'; $array = array(); parse_str(str_replace(';','&',rtrim($string,';')), $array); Ja ērtāk, tad strings var būt arī: balts=2;melns=3;sarkans=5; Edited November 8, 2010 by indoom 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.