Incredinot Posted February 10, 2010 Share Posted February 10, 2010 Alright... Im getting some errors with my foreach loop.. Can anyone tell me whats wrong with it? It does print like it should correctly, but i get this error also: Invalid argument supplied for foreach() in C:\xxxxx\xxxxx\xxxxx on line xxx. Note: It do print the things it's supposed to - but the error comes anyway? o.O Here is the code: <?php foreach($field_foo as $hai) {print $hai['value'] . ", ";}?> And i will also like to write ", " while there is more.. But if its the last, i just want a ".". Hope you understand (: Regards, Incredinot Quote Link to comment https://forums.phpfreaks.com/topic/191594-help-with-simple-foreach/ Share on other sites More sharing options...
trq Posted February 10, 2010 Share Posted February 10, 2010 Providing $field_foo is an array (which the error suggests its not), it would be much simpler & more efficient to use implode. <?php echo implode(', ', $field_foo); ?> Quote Link to comment https://forums.phpfreaks.com/topic/191594-help-with-simple-foreach/#findComment-1009953 Share on other sites More sharing options...
PravinS Posted February 10, 2010 Share Posted February 10, 2010 First check you array $field_foo, is it valid array? Then for "," issue here is the code <?php $str = ""; foreach($field_foo as $hai) { $str = $hai['value'] . ","; } $str = substr($str,0,-1); print $str; ?> Quote Link to comment https://forums.phpfreaks.com/topic/191594-help-with-simple-foreach/#findComment-1009954 Share on other sites More sharing options...
Incredinot Posted February 10, 2010 Author Share Posted February 10, 2010 Providing $field_foo is an array (which the error suggests its not), it would be much simpler & more efficient to use implode. <?php echo implode(', ', $field_foo); ?> If i use the above code, it just print "Array, Array, Array, Array " And if i write something like: <?php echo implode(', ', $field_foo['value']); ?> i get nothing.. Quote Link to comment https://forums.phpfreaks.com/topic/191594-help-with-simple-foreach/#findComment-1009958 Share on other sites More sharing options...
trq Posted February 10, 2010 Share Posted February 10, 2010 $field_foo is obviously a multidimensional array then. What is the output of..... print_r($field_foo); ? Quote Link to comment https://forums.phpfreaks.com/topic/191594-help-with-simple-foreach/#findComment-1009959 Share on other sites More sharing options...
teamatomic Posted February 10, 2010 Share Posted February 10, 2010 Dont really know what your array looks like but see if this works. <?php $i=1; $num=count($field_foo); foreach($field_foo as $hai) { if ($i != $num) { print $hai['value'] . ", "; } else { print $hai['value'] . "."; } $i++; } ?> HTH Teamatomic Quote Link to comment https://forums.phpfreaks.com/topic/191594-help-with-simple-foreach/#findComment-1009963 Share on other sites More sharing options...
Incredinot Posted February 10, 2010 Author Share Posted February 10, 2010 This is what i get from: <pre><?php print_r($node->field_spillesteder);?></pre> Array ( [0] => Array ( [value] => Vestjylland [safe] => Vestjylland [view] => Vestjylland ) [1] => Array ( [value] => Nordjylland [safe] => Nordjylland [view] => Nordjylland ) [2] => Array ( [value] => Sjælland [safe] => Sjælland [view] => Sjælland ) [3] => Array ( [value] => Fyn [safe] => Fyn [view] => Fyn ) ) Quote Link to comment https://forums.phpfreaks.com/topic/191594-help-with-simple-foreach/#findComment-1009965 Share on other sites More sharing options...
trq Posted February 10, 2010 Share Posted February 10, 2010 So, what exactly do you want as output? Its obvious these array element can't simply be joined by a , Quote Link to comment https://forums.phpfreaks.com/topic/191594-help-with-simple-foreach/#findComment-1009966 Share on other sites More sharing options...
Incredinot Posted February 10, 2010 Author Share Posted February 10, 2010 Alright, let me explain (: I have this "page". When i create a page i have a select box where i can select different things. To be exact it is: Sydjylland Vestjylland Midtjylland Nordjylland Sjælland Fyn Bornholm In the above example, i have chosen "Vestjylland, Nordjylland, Sjælland, Fyn". Now i want to output that this it what i have chosen. Hope you understand Quote Link to comment https://forums.phpfreaks.com/topic/191594-help-with-simple-foreach/#findComment-1009976 Share on other sites More sharing options...
trq Posted February 10, 2010 Share Posted February 10, 2010 Can we see the code for the select? Quote Link to comment https://forums.phpfreaks.com/topic/191594-help-with-simple-foreach/#findComment-1009984 Share on other sites More sharing options...
Incredinot Posted February 10, 2010 Author Share Posted February 10, 2010 <select multiple="multiple" name="field_spillesteder[value][]"> <option value="Sydjylland">Sydjylland</option> <option selected="selected" value="Vestjylland">Vestjylland</option> <option value="Midtjylland">Midtjylland</option> <option selected="selected" value="Nordjylland">Nordjylland</option> <option selected="selected" value="Sjælland">Sjælland</option> <option selected="selected" value="Fyn">Fyn</option> <option value="Bornholm">Bornholm</option> </select> Quote Link to comment https://forums.phpfreaks.com/topic/191594-help-with-simple-foreach/#findComment-1009988 Share on other sites More sharing options...
Incredinot Posted February 10, 2010 Author Share Posted February 10, 2010 anything new? (: Quote Link to comment https://forums.phpfreaks.com/topic/191594-help-with-simple-foreach/#findComment-1010034 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.