Jump to content

Help with arrays.


jacob1986

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
Share on other sites


$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
Link to comment
Share on other sites

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
Link to comment
Share on other sites

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;
Link to comment
Share on other sites

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

Link to comment
Share on other sites

 

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.

Link to comment
Share on other sites

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.