1

My code is below.

1) I needed to get the number of words in each message in the message array and display it in a new array

2) If the number of words in each message was less than 5 I am supposed to add the words "smaller" to another new array and if the number of words in each message is greater than 5 I an supposed to add the string"bigger" to this same new array.

I am unsure of how to put this "bigger" and "smaller" into a new array. Please advise.(refer question 2)

$messages = array(
    "Learning PHP is fun",
    "I want to learn more",
    "Girls can code too",
    "Goodbye boredom!",
    "Coding Geek",
    "Computers can sometimes freeze up",
    "Follow me now", "Coding is cool",
    "Computer nerds are for real",
    "This is the end of all the messages"
);

/* (1) */ 
for ($i = 0; $i < 10; $i++) {
    $words[] = str_word_count($messages[$i]);
}
print_r($words);
echo "<br>";

/* (2) */
for ($i = 0; $i < 10; $i++) {
    if ($words[$i] <= 5) {
        $size[] = $words[$i];
        echo "smaller" . "<br>";
    } else {
        $size[] = $words[$i];
        echo"bigger";
    }
}

3 Answers 3

1

First, you can use foreach instead of for loop in this case.

Second, this code is enough.Try it :

foreach($messages as $val){
    $len = str_word_count($val);
    if($len<=5){
        $msg =  "bigger";
    }else{
        $msg =  "smaller";
    }
    $size[] = array("size"=>$len, "msg"=>$msg);
}

# Your $size array would be like :
# array( 
#          array("size"=>3, "msg"=>"smaller"),
#          array("size"=>8, "msg"=>"bigger") 
#    )
# And you can print it or use it everywhere.
Sign up to request clarification or add additional context in comments.

3 Comments

this is a neat piece of code. Thankyou. Is there any way to store the "bigger" and "smaller" back in an array format instead of printing values one below the other . Just wondering.....
kind of works. Problem is the word "array" gets appended everytime a new element is added to $size[] and that is incorrect because you are not creating a new array everytime.
Yes, I'm creating new array every time, because it has two index, size and msg
0

for loops work fine for arrays. You could think about using a variable which holds the length of the original array to use in the loop.

$numberOfMessages = count($messages);

$for ($i = 0; $i < $numberOfMessages; $i ++) {
...
}

My preferred way of doing this is the same as the first two examples using a foreach loop.

foreach ($messages as $message) {
    $words[] = str_word_count($message);
}

This will create your first new array called $words by counting the number of words in each value ($message) from the original array ($messages). Using the <pre> tag formats the echo and makes it more readable.

echo '<pre>' . print_r($words, true) . '</pre>';

Your second new array is made is a similar way.

foreach ($words as $word) {
    if ($word < 5) {
        $size[] = 'smaller';
    } else {
        $size[] = 'bigger';
    }
}

This time we take each value ($word) from our new $words array and check to see what size it is and populate the second new array $size. bablu's example using the ternary operator ?: This example is just the long-hand way of writing the same conditional statement.

Putting it all together

$messages = array (
    'Learning PHP is fun',
    'I want to learn more',
    'Girls can code too',
    'Goodbye boredom!',
    'Coding Geek',
    'Computers can sometimes freeze up',
    'Follow me now', 'Coding is cool',
    'Computer nerds are for real',
    'This is the end of all the messages'
);

foreach ($messages as $message) {
    $words[] = str_word_count($message);
}

echo '<pre>' . print_r($words, true) . '</pre>';

foreach ($words as $word) {
    if ($word < 5) {
        $size[] = 'smaller';
    } else {
        $size[] = 'bigger';
    }
}

echo '<pre>' . print_r($size, true) . '</pre>';

1 Comment

perfectly explained. Thanks much !
0

Try this: its working fine for me:

<?php
$messages=array
("Learning PHP is fun","I want to learn more","Girls can code too","Goodbye boredom!","Coding Geek","Computers can sometimes freeze up","Follow me now","Coding is cool","Computer nerds are for real","This is the end of all the messages");

$newMessageArray = array();
$newCountArray = array();
foreach($messages as $message) {
    $length = str_word_count($message);

    $newMessageArray[] = $message;
    $newCountArray[] = ($length >=5)?'Bigger':'Smaller';
}

    echo '<pre>'; print_r($newMessageArray);
    echo '<pre>'; print_r($newCountArray);
?>

2 Comments

this code I am liking except that I haven't come across the syntax ($length>=5)?'Bigger':'Smaller';
This is ternary operator.. It is just like if condition value wil be set as Bigger if length greater than 5 or will be taken as smaller

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.