ericburnard Posted April 27, 2014 Share Posted April 27, 2014 I seem to be struggling massively with this little problem. im sure its just a little something I am missing but it has be winding me up for hours. This is the said code. <? $id = $_POST['id']; $tags = $_POST['tags']; foreach( $id as $n ) { $tags = $tags[$n]; echo '<br><br>'.$tags.'---'.$n.'<br>'; $tags = explode(", ", $tags); $num=count($tags); echo "$num<br>"; foreach( $tags as $tag ) { print "ID: ".$n." --- tag: ".$tag." <br>\n"; } } ?> It finishes one full loop through fine, separates out all of the tags and puts them with the correct id. But when it moves onto the second full loop it seems to be getting nothing out of here $tags = $tags[$n]; I have tried changing the $n for one of the id numbers to make sure it is pulling the data from the $_POST[] and it is. The following is the results I am getting. cheese, more cheese, even more cheese---409 3 ID: 409 --- tag: cheese ID: 409 --- tag: more cheese ID: 409 --- tag: even more cheese ---287 1 ID: 287 --- tag: ---288 1 ID: 288 --- tag: ---406 1 ID: 406 --- tag: ---407 1 ID: 407 --- tag: ---408 1 ID: 408 --- tag: Any help or pointers in the right direction would be a massive help. Eric Quote Link to comment https://forums.phpfreaks.com/topic/288064-using-explode-with-a-foreach-loop/ Share on other sites More sharing options...
Ch0cu3r Posted April 27, 2014 Share Posted April 27, 2014 You are overwriting the original value of $tags twice in the foreach loop. You'll need to use a different variable name. <?php $id = $_POST['id']; $tags = $_POST['tags']; foreach( $id as $n ) { $tag_list = $tags[$n]; echo '<br><br>'.$tag_list .'---'.$n.'<br>'; $tag_list_array = explode(", ", $tags_list); $num=count($tags); echo "$num<br>"; foreach( $tag_list_array as $tag ) { print "ID: ".$n." --- tag: ".$tag." <br>\n"; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/288064-using-explode-with-a-foreach-loop/#findComment-1477451 Share on other sites More sharing options...
Solution ericburnard Posted April 27, 2014 Author Solution Share Posted April 27, 2014 Always something so simple. I think I have been overwriting my brain as well today! You're a superstar Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/288064-using-explode-with-a-foreach-loop/#findComment-1477452 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.