Jump to content

Recommended Posts

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!
Link to comment
https://forums.phpfreaks.com/topic/298549-help-with-arrays/
Share on other sites

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!'
];

...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


$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 />';
}
  • Like 1

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 by scootstah

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;

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

 

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.