Rich Bitch Posted June 20, 2010 Report Share Posted June 20, 2010 (edited) Taatad man ir "multilevel" masiivs, kas tiek izveidots no datu baazes izguustot datus. 3 liimenii (tur kur veertiiba 6, bet citreiz tas var notikt liimeni augstaak vai zemaak) notiek paarbaude der vai neder shii veertiiba. Ja veertiiba neder, tad shii masiiva konkreetais ieraksts "6" tiek dzeests. Ja "parent" masiiva elementam shis ir bijis vieniigais ieraksts, tad dzeeshas arii parent elements. Un taa tas atkaartojas visus liimenjus uz augshu, kameer kaadam no parent elementiem veel buus kaads child elements. Nesaprotu kaa shaadu sisteemu izveidot. Probleemas sagaadaa parent liimenja dzeeshana, jo nav iespeejams uzzinaat taa "masiiva adresi" (6 parent elementam taa buus $test[2][1][0]). Kaa lai "dabuj" sho masiiva adresi? Masīva piemērs: $test = Array ( [0] => 1 [1] => 2 [2] => Array ( [0] => 1 [1] => Array ( [0] => Array ( [0] => 6 ) ) ) ) Edited June 20, 2010 by Rich Bitch Quote Link to comment Share on other sites More sharing options...
briedis Posted June 20, 2010 Report Share Posted June 20, 2010 (edited) Kā iztēlojos, ir divi varianti - rekursīva funkcija, vai vairāku līmeņu foreach cikli (ja fiksēts līmeņu skaits)... Ar vairākiem cikliem būtu vienkāršāk, bet vairāk koda... Ar rekursiju - nāksies nedaudz palauzīt galvu, lai izštukotu ko un kā... Kā izstāstīt tev domu, neuzrakstot visu priekšā - nezinu. Tev ir doti diezgan spēcīgi instrumenti - foreach cikls, is_array, count, unset funkciojas, if pārbaudes, break nosacījums, iespēja neierobežoti definēt mainīgos... Ar to arī pietiek, lai realizētu vajadzīgo, tik smadzenes jākustina, jāmēģina. Esi jau kaut ko uzrakstījis? Rekur uzcepu nesmuku kodu, kas dzēš pamata vecāku, ja 3. līmeņa bērns ir 6... $test = Array(1, 2, Array(1, Array( Array(6) ) ) ); foreach($test as $key => $level1){ $delete_index = false; if(is_array($level1)){ foreach($level1 as $level2){ if(is_array($level2)){ foreach($level2 as $level3){ foreach($level3 as $items){ if($items == 6){ $delete_index = $key; } } } } } } if($delete_index !== false){ unset($test[$delete_index]); } } Edited June 20, 2010 by briedis Quote Link to comment Share on other sites More sharing options...
Rich Bitch Posted June 20, 2010 Author Report Share Posted June 20, 2010 Liimenju skaits var buut neierobezhots. Quote Link to comment Share on other sites More sharing options...
briedis Posted June 20, 2010 Report Share Posted June 20, 2010 Nu tad vienīgā izeja ir rekursīvi to darīt... Vienīgi, sūdīgi, ja nezini, kas ir rekursija :) Quote Link to comment Share on other sites More sharing options...
spainis Posted June 20, 2010 Report Share Posted June 20, 2010 function delNodes(& $array, $sValue) { foreach($array as $key => $value) { if(is_array($value)) { if(delNodes($value, $sValue)) { unset($array[$key]); return true; } } else { if($value == $sValue) { unset($array[$key]); return true; } } } return false; } Quote Link to comment Share on other sites More sharing options...
Rich Bitch Posted June 20, 2010 Author Report Share Posted June 20, 2010 Vienīgi, sūdīgi, ja nezini, kas ir rekursija :) nezinaashu gan kas tas ir Quote Link to comment Share on other sites More sharing options...
waplet Posted June 20, 2010 Report Share Posted June 20, 2010 Ja nemaldos, tad tas izpaužas šādi function funkcija($a){ $a++; if($a < 6){ funkcija($a); }else{ return $a; } } Quote Link to comment Share on other sites More sharing options...
spainis Posted June 20, 2010 Report Share Posted June 20, 2010 tagad izdzēsīs tikai tos, zarus, kuri ir pilnībā tukši /** * @param Array $array Masīvs kurā meklēt * @param mixed $sValue Meklējamā vērtība * @return Array satīrītais masīvs */ function delNodes($array, $sValue) { foreach($array as $key => $value) { if(is_array($value)) { $array[$key] = delNodes($value, $sValue); if(empty($array[$key])) { unset($array[$key]); } } else { if($value == $sValue) { unset($array[$key]); } } } return $array; } Quote Link to comment Share on other sites More sharing options...
Rich Bitch Posted June 20, 2010 Author Report Share Posted June 20, 2010 (edited) Ok, shim doma skaidra. ar shito viss izdevaas, bet tagad atduuros pret to, ka izraadaas man masiivs nepareizi veidojas. censhos cik speeju, bet neizdoas to dabuut pareizu. dereetu paliidziba no malas taatad ir db ar tabulu (id, starts, beigas). tabulaa staav, pienjemsim, virzieni. Lai vieglaak saprast, tad tie buus virzini uz pilseetaam kaa sabiedriskajam transportam. Piemeeram, starts Riga, beigas Daugavpils utt. Vajadzeetu dabuut visus iespeejamos veidos, kaa nokljuut no Riigas uz Daugavpili (Riga-Daugavpils, Riga-Ogre-Daugavpils, Riga-Jekabpils-Daugavpils utml.). Paraugam tabulas saturs shaads: 1, Riga, Daugavpils 2, Riga, Ogre 3, Riga, Jekabpils 4, Ogre, Daugavpils 5, Ogre, Jekabpils 6, Jekabpils, Daugavpils No shiis tabulas vajag izguut masiivu, kas satureetu apakshmasiivus ar shiem pilnajiem virzieniem (virzieni nedriikst atkaartoties): $test = array( 1 => 1, 2 => array ( 2 => 4, ) 3 => array ( 3 => 6 ) ) es to meegjinu shaadi, bet nu galiig neriktiigi $starts = "Riga"; $beigas = "Daugavpils"; $virzieni = array(); $_SESSION["used"] = array(); $varizieni = dabut_virzienus($virzieni, $starts, $beigas); function dabut_virzienus($virzieni, $starts, $beigas) { if(sizeof($_SESSION["used"]) > 0) { $used_sql = " AND id !="; } else { $used_sql = ""; } $used_sql .= implode(" AND id != ", $_SESSION["used"]); $sql = mysql_query("SELECT * FROM virzieni WHERE `starts` = '".$starts."'".$used_sql) or die(mysql_error()); if(mysql_num_rows($sql) > 0) { while($row = mysql_fetch_array($sql)) { if(!in_array($row["id"], $_SESSION["used"])) { $_SESSION["used"][] = $row["id"]; if($row["beigas"] != $beigas) { $virzieni[$row["id"]] = dabut_virzienus($virzieni, $row["beigas"], $beigas); } else { $virzieni[$row["id"]] = $row["id"]; } } } } else { // te taa funkcija, kas dzeesh araa tos parent massivus } } Edited June 20, 2010 by Rich Bitch Quote Link to comment Share on other sites More sharing options...
Toms Posted June 20, 2010 Report Share Posted June 20, 2010 (edited) Kā lai es zinu vai stacija Ogre ir pa vidu starp Rīga-Daugavpils? Un nav uz Rīga-Ventspils maršruta. Edited June 20, 2010 by Toms Quote Link to comment Share on other sites More sharing options...
Rich Bitch Posted June 20, 2010 Author Report Share Posted June 20, 2010 (edited) Nevar zinaat. Nav svariiga atrashanaas vieta, bet tas vai caur sho vietu var nokluut vai nee. Edited June 20, 2010 by Rich Bitch Quote Link to comment Share on other sites More sharing options...
Toms Posted June 20, 2010 Report Share Posted June 20, 2010 Varbūt kaut kas šāds tev palīdzēs select t2.*,t1.* from (select * from t where sakums='Riga' or beigas='Daugavpils') as t1, (select * from t where sakums='Riga' or beigas='Daugavpils') as t2 where t1.sakums=t2.beigas; 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.