shadd Posted September 2 Share Posted September 2 i have this string of ids: $p_id=1.1.51|||1.1.51.2|||1.1.51.3|||1.1.51.4|||1.1.51.5|||1.1.51.6|||1.1.51.7|||1.1.51.8|||1.1.51.9|||1.1.52|||1.1.51.10, 1.1.52|||, i explode it like so: $Q_Id=explode('|||',p_id); i have another string of themes like so: $theme='b|||c|||d|||e|||f|||g|||h|||i|||j,' which i explode like so: $sub=explode(',',$theme); each element in theme string corresponds to an id in the string above delimited by a comma. therefore i add a to the array for($s=0;$s<count($sub);$s++){ $DisplaySub[$s]=explode('|||',$sub[$s]); //add a to startnof array array_unshift($DisplaySub ,'a'); } how can I loop through the ids and display their corresponding list of themes using for/foreach loop? for ($i = 0; $i < count($Q_Id); $i++){ for($s=0;$s<count($sub);$s++){ $DisplaySub[$s]=explode('|||',$sub[$s]); //add a to startnof array array_unshift($DisplaySub ,'a'); } } how can i merge the corresponding $sub theme to a specific id say 1.1.51: a 1.1.51.2:b . . 1.1.52:a 1.1.52.2:b etc... Quote Link to comment Share on other sites More sharing options...
jodunno Posted September 2 Share Posted September 2 what kind of train wreck code is that? holy and moly and mother of godsmack. so with the limited splatter that you have provided along with empty array values (id est, value ||| value, ), we can only do so much to help you out. <?php $p_id='1.1.51|||1.1.51.2|||1.1.51.3|||1.1.51.4|||1.1.51.5|||1.1.51.6|||1.1.51.7|||1.1.51.8|||1.1.51.9|||1.1.52|||1.1.52.2'; $Q_Id = explode('|||', $p_id); $m = count($Q_Id); $theme = 'b|||c|||d|||e|||f|||g|||h|||i|||j|||k'; $sub = explode(',', $theme); $sub = explode('|||', $sub[0]); $sub2[] = 'a'; $sub = array_merge($sub2, $sub); $n = count($sub) - 1; //print_r($Q_Id); echo '<br>'; //print_r($sub); echo '<br>'; $j = 0; for ($i = 0; $i < $m; $i++) { if ($Q_Id[$i] === '1.1.52') { $j = 0; } echo $Q_Id[$i] . ': ' . $sub[$j] . '<br>'; $j++; } ?> you really must stop using count in a loop. For the love of PHP just make a variable to hold the count. Also, i need glasses because my eyes are getting bad with age but i still can't read your code because of the lack of spaces. Add dome spaces to make it more legible. Your code is like a messy room. Yakety Yak! Take out the papers and the trash ... Quote Link to comment Share on other sites More sharing options...
Barand Posted September 2 Share Posted September 2 2 hours ago, shadd said: $theme='b|||c|||d|||e|||f|||g|||h|||i|||j,' which i explode like so: $sub=explode(',',$theme); Why explode on comma when they too are delimited by '|||' ? 2 hours ago, shadd said: each element in theme string corresponds to an id in the string above If that is the case (although your example says otherwise) then you can use array_combine() to store the related values EG $p_id = "1.1.51|||1.1.51.2|||1.1.51.3|||1.1.51.4|||1.1.51.5|||1.1.51.6|||1.1.51.7|||1.1.51.8|||1.1.51.9"; $theme = 'b|||c|||d|||e|||f|||g|||h|||i|||j'; $i = explode('|||', $p_id); $t = explode('|||', $theme); $result = array_combine($i, $t); foreach ($result as $id => $theme) { echo "$id : $theme <br>"; } giviing 1.1.51 : b 1.1.51.2 : c 1.1.51.3 : d 1.1.51.4 : e 1.1.51.5 : f 1.1.51.6 : g 1.1.51.7 : h 1.1.51.8 : i 1.1.51.9 : j Quote Link to comment Share on other sites More sharing options...
shadd Posted September 2 Author Share Posted September 2 29 minutes ago, Barand said: Why explode on comma when they too are delimited by '|||' ? If that is the case (although your example says otherwise) then you can use array_combine() to store the related values EG $p_id = "1.1.51|||1.1.51.2|||1.1.51.3|||1.1.51.4|||1.1.51.5|||1.1.51.6|||1.1.51.7|||1.1.51.8|||1.1.51.9"; $theme = 'b|||c|||d|||e|||f|||g|||h|||i|||j'; $i = explode('|||', $p_id); $t = explode('|||', $theme); $result = array_combine($i, $t); foreach ($result as $id => $theme) { echo "$id : $theme <br>"; } giviing 1.1.51 : b 1.1.51.2 : c 1.1.51.3 : d 1.1.51.4 : e 1.1.51.5 : f 1.1.51.6 : g 1.1.51.7 : h 1.1.51.8 : i 1.1.51.9 : j $theme='b|||c|||d|||e|||f|||g|||h|||i|||j,b|||c|||d|||e|||f|||g|||h|||i|||j'; the first string:b|||c|||d|||e|||f|||g|||h|||i|||j is for the id 1.1.51.......1.1.51.10 and the second string:b|||c|||d|||e|||f|||g|||h|||i|||j is for the next id 1.1.52.........1.1.52.10 so $p_id = "1.1.51|||1.1.51.2|||1.1.51.3|||1.1.51.4|||1.1.51.5|||1.1.51.6|||1.1.51.7|||1.1.51.8|||1.1.51.9|||1.1.52|||1.1.52.2|||1.1.52.3|||1.1.52.4|||1.1.52.5|||1.1.52.6|||1.1.52.7|||1.1.52.8|||1.1.52.9"; But some Ids may not have a theme that is it may be empty Quote Link to comment Share on other sites More sharing options...
shadd Posted September 2 Author Share Posted September 2 58 minutes ago, jodunno said: what kind of train wreck code is that? holy and moly and mother of godsmack. so with the limited splatter that you have provided along with empty array values (id est, value ||| value, ), we can only do so much to help you out. <?php $p_id='1.1.51|||1.1.51.2|||1.1.51.3|||1.1.51.4|||1.1.51.5|||1.1.51.6|||1.1.51.7|||1.1.51.8|||1.1.51.9|||1.1.52|||1.1.52.2'; $Q_Id = explode('|||', $p_id); $m = count($Q_Id); $theme = 'b|||c|||d|||e|||f|||g|||h|||i|||j|||k'; $sub = explode(',', $theme); $sub = explode('|||', $sub[0]); $sub2[] = 'a'; $sub = array_merge($sub2, $sub); $n = count($sub) - 1; //print_r($Q_Id); echo '<br>'; //print_r($sub); echo '<br>'; $j = 0; for ($i = 0; $i < $m; $i++) { if ($Q_Id[$i] === '1.1.52') { $j = 0; } echo $Q_Id[$i] . ': ' . $sub[$j] . '<br>'; $j++; } ?> you really must stop using count in a loop. For the love of PHP just make a variable to hold the count. Also, i need glasses because my eyes are getting bad with age but i still can't read your code because of the lack of spaces. Add dome spaces to make it more legible. Your code is like a messy room. Yakety Yak! Take out the papers and the trash ... What if you do not know the next id i.e it is auto generated and you just have to loop through.i.e you do not know 1.1.52 is the next,what comparison would you use here to return to $j=0 for ($i = 0; $i < $m; $i++) { if ($Q_Id[$i] === '1.1.52') { $j = 0; } echo $Q_Id[$i] . ': ' . $sub[$j] . '<br>'; $j++; } Quote Link to comment Share on other sites More sharing options...
Solution jodunno Posted September 2 Solution Share Posted September 2 (edited) 46 minutes ago, shadd said: What if you do not know the next id i.e it is auto generated and you just have to loop through.i.e you do not know 1.1.52 is the next,what comparison would you use here to return to $j=0 for ($i = 0; $i < $m; $i++) { if ($Q_Id[$i] === '1.1.52') { $j = 0; } echo $Q_Id[$i] . ': ' . $sub[$j] . '<br>'; $j++; } I would not design a site in such a manner. I would recognize the errors in my ways and fix them. 'been there, done that' is a common expression that applies here. But because you asked: <?php $p_id='1.1.51|||1.1.51.2|||1.1.51.3|||1.1.51.4|||1.1.51.5|||1.1.51.6|||1.1.51.7|||1.1.51.8|||1.1.51.9|||1.1.52|||1.1.52.2'; $Q_Id = explode('|||', $p_id); $m = count($Q_Id); $theme = 'b|||c|||d|||e|||f|||g|||h|||i|||j|||k'; $sub = explode(',', $theme); $sub = explode('|||', $sub[0]); $sub2[] = 'a'; $sub = array_merge($sub2, $sub); $n = count($sub) - 1; //print_r($Q_Id); echo '<br>'; //print_r($sub); echo '<br>'; $j = 0; $k = substr($Q_Id[0], 0, 6); for ($i = 0; $i < $m; $i++) { $index = substr($Q_Id[$i], 0, 6); if ($index !== $k) { $j = 0; $k = substr($Q_Id[$i], 0, 6); echo '----- looky here ' . $k . '<br>'; } echo $Q_Id[$i] . ': ' . $sub[$j] . '<br>'; $j++; } ?> I do not understand why you have these ids in a variable. You should store them in an array by an index system so that you know when to start back at the letter a. However, the English alphabet runs out of letters at 26 so do you plan on having a solution for that too? Do you see how such code leads us down a dark spiraling vertex of madness? a black hole. Even light cannot escape this madness. Change your code as soon as possible. Meantime, good luck with all of that and goodnight. I am in a different timezone and it's almost bedtime. edit: to avoid more questions, $k has been set to the value of array key 0. Edited September 2 by jodunno changed inital value of var k 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.