jacob1986 Posted October 12, 2015 Share Posted October 12, 2015 I have to declare an array which contains the sentence 'Programming in PHP is fun!' and which also contains the words of the sentence separately. I have the following code but the end sentence has a zero - how do I get rid of that, moreover, how do I put a line break in between the line 'fifth word fun! and Programming in PHP is fun! <?php $fun = [ 'First Word' => 'Programming', 'Second Word' => 'in', 'Third Word' => 'PHP', 'Fourth Word' => 'is', 'Fifth Word' => 'fun!', 'Programming in PHP is fun!' ]; foreach($fun as $key=>$val) { echo" $key, $val</br>";} ?> Output: First Word, ProgrammingSecond Word, inThird Word, PHPFourth Word, isFifth Word, fun!0, Programming in PHP is fun! Quote Link to comment https://forums.phpfreaks.com/topic/298549-help-with-arrays/ Share on other sites More sharing options...
cyberRobot Posted October 12, 2015 Share Posted October 12, 2015 You could specify a key for the last element of the array. $fun = [ 'First Word' => 'Programming', 'Second Word' => 'in', 'Third Word' => 'PHP', 'Fourth Word' => 'is', 'Fifth Word' => 'fun!', 'Full Sentence' => 'Programming in PHP is fun!' ]; Quote Link to comment https://forums.phpfreaks.com/topic/298549-help-with-arrays/#findComment-1523027 Share on other sites More sharing options...
cyberRobot Posted October 12, 2015 Share Posted October 12, 2015 ...how do I put a line break in between the line 'fifth word fun! and Programming in PHP is fun! You could use an if construct to test $key in the foreach loop. Then just insert a line break when the 5th item is found. http://php.net/manual/en/control-structures.if.php Quote Link to comment https://forums.phpfreaks.com/topic/298549-help-with-arrays/#findComment-1523029 Share on other sites More sharing options...
jacob1986 Posted October 12, 2015 Author Share Posted October 12, 2015 Thank-you. Quote Link to comment https://forums.phpfreaks.com/topic/298549-help-with-arrays/#findComment-1523032 Share on other sites More sharing options...
hansford Posted October 12, 2015 Share Posted October 12, 2015 $fun = array( 'First Word' => 'Programming', 'Second Word' => 'in', 'Third Word' => 'PHP', 'Fourth Word' => 'is', 'Fifth Word' => 'fun!', '' => 'Programming in PHP is fun!' ); foreach($fun as $key => $val) { if($key == '') { echo $val; } else { echo "$key, $val"; } echo '<br />'; } 1 Quote Link to comment https://forums.phpfreaks.com/topic/298549-help-with-arrays/#findComment-1523043 Share on other sites More sharing options...
scootstah Posted October 12, 2015 Share Posted October 12, 2015 (edited) Just for giggles: $fun = array( 'First Word' => 'Programming', 'Second Word' => 'in', 'Third Word' => 'PHP', 'Fourth Word' => 'is', 'Fifth Word' => 'fun!', 'Programming in PHP is fun!' ); foreach($fun as $key => $val) { if ($val != end(array_values($fun))) { echo "$key, "; } echo "$val<br />"; } Edited October 12, 2015 by scootstah Quote Link to comment https://forums.phpfreaks.com/topic/298549-help-with-arrays/#findComment-1523047 Share on other sites More sharing options...
Psycho Posted October 12, 2015 Share Posted October 12, 2015 Well, if we're just showing different ways to skin a cat: $fun = array( 'First Word' => 'Programming', 'Second Word' => 'in', 'Third Word' => 'PHP', 'Fourth Word' => 'is', 'Fifth Word' => 'fun!', 'Programming in PHP is fun!' ); $fullSentence = array_pop($fun); foreach($fun as $key => $val) { echo "$key, $val<br />"; } echo $fullSentence; Quote Link to comment https://forums.phpfreaks.com/topic/298549-help-with-arrays/#findComment-1523050 Share on other sites More sharing options...
scootstah Posted October 12, 2015 Share Posted October 12, 2015 Calling on requinix. 10 characters or less, go! Quote Link to comment https://forums.phpfreaks.com/topic/298549-help-with-arrays/#findComment-1523052 Share on other sites More sharing options...
benanamen Posted October 12, 2015 Share Posted October 12, 2015 @hansford, nice, clean answer. Quote Link to comment https://forums.phpfreaks.com/topic/298549-help-with-arrays/#findComment-1523053 Share on other sites More sharing options...
benanamen Posted October 12, 2015 Share Posted October 12, 2015 Just for giggles: $fun = array( 'First Word' => 'Programming', 'Second Word' => 'in', 'Third Word' => 'PHP', 'Fourth Word' => 'is', 'Fifth Word' => 'fun!', 'Programming in PHP is fun!' ); foreach($fun as $key => $val) { if ($val != end(array_values($fun))) { echo "$key, "; } echo "$val<br />"; } I get this running your example: Only variables should be passed by reference on line number 14 Quote Link to comment https://forums.phpfreaks.com/topic/298549-help-with-arrays/#findComment-1523054 Share on other sites More sharing options...
benanamen Posted October 12, 2015 Share Posted October 12, 2015 Well, if we're just showing different ways to skin a cat: $fun = array( 'First Word' => 'Programming', 'Second Word' => 'in', 'Third Word' => 'PHP', 'Fourth Word' => 'is', 'Fifth Word' => 'fun!', 'Programming in PHP is fun!' ); $fullSentence = array_pop($fun); foreach($fun as $key => $val) { echo "$key, $val<br />"; } echo $fullSentence; Interesting take, although it does require that you know that the last element is the one without a key. Quote Link to comment https://forums.phpfreaks.com/topic/298549-help-with-arrays/#findComment-1523055 Share on other sites More sharing options...
benanamen Posted October 12, 2015 Share Posted October 12, 2015 Speaking of "Skinning a Cat", lets all head on over here for some fun. http://forums.phpfreaks.com/topic/298555-skinning-a-cat-or-how-many-ways-to-output-hello-world/ Quote Link to comment https://forums.phpfreaks.com/topic/298549-help-with-arrays/#findComment-1523057 Share on other sites More sharing options...
scootstah Posted October 12, 2015 Share Posted October 12, 2015 I get this running your example: Only variables should be passed by reference on line number 14[/size] Bummer. Silly PHP. Quote Link to comment https://forums.phpfreaks.com/topic/298549-help-with-arrays/#findComment-1523058 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.