latino.ad7 Posted February 22, 2010 Share Posted February 22, 2010 How can I number a variable within a loop based on another variable? Here is a sample PHP code: foreach ($ar3id as $w) { foreach ($ar4id as $w2) { echo '&whatever'; } } Variable $w is an integer with some values, let's say: 2, 4, 5, 8. Now how can I make a blue 4 a variable $w? What is the syntax? Quote Link to comment https://forums.phpfreaks.com/topic/192998-how-can-i-number-a-variable-within-a-loop/ Share on other sites More sharing options...
ialsoagree Posted February 22, 2010 Share Posted February 22, 2010 There might be a way to do this using eval (which I'm myself not too familiar with). I would suggest, instead, using an array whose index would be contained in $w. So for example... $array1[] = 4; $someArray[4][] = 'what'; $someArray[4][] = 'ever'; foreach ($array1 as $w) { foreach ($someArray[$w] as $w2) { echo $w2; } }// Would echo: whatever Quote Link to comment https://forums.phpfreaks.com/topic/192998-how-can-i-number-a-variable-within-a-loop/#findComment-1016404 Share on other sites More sharing options...
latino.ad7 Posted February 22, 2010 Author Share Posted February 22, 2010 No, no. That's not what I meant. I would like a number contained in the name of the variable to be dynamic based on the value of another variable. In the loop this would go: foreach ($ar4id as $w2) foreach ($ar6id as $w2) foreach ($ar9id as $w2) etc. So how do I type: foreach ($ar/$w/id as @w2) {} ???? With text I would merge it: 'ar'.$w.'id', but it's a variable within a variable. Quote Link to comment https://forums.phpfreaks.com/topic/192998-how-can-i-number-a-variable-within-a-loop/#findComment-1016409 Share on other sites More sharing options...
ialsoagree Posted February 22, 2010 Share Posted February 22, 2010 I understand what you're asking, I'm merely remarking that (as far as I'm aware) PHP does not support this type of behavior without the use of a function. The eval function might be able to do this for you but I'm not very familiar with it. You can wait for someone else to reply possibly with how to use eval, or you can look up the function yourself. Otherwise, it may be easier to use an array as I've described above. Quote Link to comment https://forums.phpfreaks.com/topic/192998-how-can-i-number-a-variable-within-a-loop/#findComment-1016411 Share on other sites More sharing options...
PFMaBiSmAd Posted February 22, 2010 Share Posted February 22, 2010 Php would let you do this using variable-variables, but you already have the data in an array(s). There is nothing wrong with using array variables directly. Why waste the processing time and memory (you would be doubling the amount of memory required unless you unset() the original data), then you must keep track of the variable names you just created. Also, accessing a variable-variable takes three-time longer than accessing an array variable (an important consideration if you have a lot of data to process.) Quote Link to comment https://forums.phpfreaks.com/topic/192998-how-can-i-number-a-variable-within-a-loop/#findComment-1016423 Share on other sites More sharing options...
ialsoagree Posted February 22, 2010 Share Posted February 22, 2010 Php would let you do this using variable-variables, but you already have the data in an array(s). There is nothing wrong with using array variables directly. Why waste the processing time and memory (you would be doubling the amount of memory required unless you unset() the original data), then you must keep track of the variable names you just created. Also, accessing a variable-variable takes three-time longer than accessing an array variable (an important consideration if you have a lot of data to process.) I tried to PM you but couldn't. Is there a PHP manual entry on variable-variables? I've not seen this before in PHP and I'm quite curious. Question answered, thanks. Quote Link to comment https://forums.phpfreaks.com/topic/192998-how-can-i-number-a-variable-within-a-loop/#findComment-1016429 Share on other sites More sharing options...
latino.ad7 Posted February 22, 2010 Author Share Posted February 22, 2010 OK, here is full code. I want to present categories and links within each category. My problem is that somehow in every category the links contain all the previous categories and the new ones. I think there is something wrong with the inner loop, that's why I was thinking to impose a variable within a variable. Perhaps there is an easier way? <?php $q3 = ("Select id, cat from cat order by id"); $r3 = mysql_query ($q3, $db); while ($row3 = mysql_fetch_array($r3, MYSQL_ASSOC)) { $ar3id[$row3['id']] = $row3['id']; $ar3cat[$row3['id']] = $row3['cat']; } $num3 = mysql_num_rows($r3); echo '<table style="BORDER-COLLAPSE: collapse" border=0 cellSpacing=0 borderColor=#000000 bgColor=#000000 cellPadding=3>'; // Loop for a Category foreach ($ar3id as $w) { echo '<tr valign="top"><td>'; echo '<basefont color="#FFFFFF">'; echo $ar3cat[$w]; echo '</basefont>'; echo '</td></tr>'; $q4 = ("Select * from kkk where cat = '".$ar3cat[$w]."' order by id"); $r4 = mysql_query ($q4, $db); while ($row4 = mysql_fetch_array($r4, MYSQL_ASSOC)) { $ar4id[$row4['id']] = $row4['id']; $ar4d[$row4['id']] = $row4['d']; $ar4l[$row4['id']] = $row4['l']; $ar4cat[$row4['id']] = $row4['cat']; $ar4com[$row4['id']] = $row4['com']; $ar4sta[$row4['id']] = $row4['sta']; } $num4 = mysql_num_rows($r4); // Loop for lines within a Category foreach ($ar4id as $w2) { echo '<tr valign="top"><td>'; echo ' <A href="'.$ar4l[$w2].'">'.$ar4d[$w2].'</A><br>'; echo $q4.' '.$ar4d[$w2].' '.$ar4id[$w2]; echo '</td>'; echo '</tr>'; } } echo '</table>'; ?> Quote Link to comment https://forums.phpfreaks.com/topic/192998-how-can-i-number-a-variable-within-a-loop/#findComment-1016433 Share on other sites More sharing options...
latino.ad7 Posted February 22, 2010 Author Share Posted February 22, 2010 Anyone has an idea? Quote Link to comment https://forums.phpfreaks.com/topic/192998-how-can-i-number-a-variable-within-a-loop/#findComment-1016454 Share on other sites More sharing options...
latino.ad7 Posted February 22, 2010 Author Share Posted February 22, 2010 Solved. unset() seems to fix it. Quote Link to comment https://forums.phpfreaks.com/topic/192998-how-can-i-number-a-variable-within-a-loop/#findComment-1016470 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.