简体   繁体   中英

No Output? PHP foreach doesn't seem to work

I am trying to form an acronym from a given text. The Idea here is that the first Letter in $text ($text[0]) will be taken and placed inside the array $storage using array_push(). Now, if there is a space inside the array, the letter of the next index should be a part of the Acronym. I am currently not getting an ouput, what am I missing?

public function Acronym($text)
        {
            $text = str_split($text);
            $count = strlen($text);
            $storage = array();

            for($i=0; $i<$count; $i++)
            {
                array_push($storage, $text[0]);

                if($text[$i]==' ')
                {
                    array_push($storage, $text[$i+1]);
                }

                foreach($storage as $clean)
                {
                    echo $clean;
                }       
            }   
        }

Your algorithm suffers from a few fatal flaws:

  1. You're calling strlen() on an array, when you should be calling count() :

     $text = str_split($text); $count = count($text); 
  2. However, you can index strings as arrays, so you don't need str_split() in this scenario, and you can keep $count = strlen( $text); by removing the call to str_split() .

  3. This should only happen once, so it should be outside the loop (This implies starting $i at 1):

     array_push($storage, $text[0]); 
  4. Your foreach loop that prints the $storage array should be outside of the loop that is creating the acronym.

  5. You can save the overhead of calling a function by using the shorthand array_push() notation. You should use array_push() when adding more than one element to an array. Otherwise, this will suffice:

     $storage[] = $text[0]; 
  6. You need to return something from your function, otherwise you won't be able to access anything outside of it.

Put that all together, and you get this:

public function Acronym($text)
{
    $count = strlen( $text);

    $storage[] = $text[0];

    for( $i = 1; $i < $count; $i++)
    {
        if( $text[$i] == ' ') 
        {
            $storage[] = $text[$i+1]);
            $i++; // Can increment $i here because we know the next character isn't a space
        }
    }
    foreach($storage as $clean)
    {
        echo $clean;
    }
    return $storage;
}

That being said, there are far better implementations for forming an acronym giving a string input. Here is one that I can think of:

public function Acronym( $text) 
{
    $acronym = array();
    foreach( explode( ' ', $text) as $word)
    {
        $word = trim( $word);
        $acronym[] = strtoupper( $word[0]);
    }
    return implode( '', $acronym);
}

Note that both functions will fail for inputs like Hello World . I am leaving it up to the OP to make these modifications (if necessary).

you are running your loop on $count which is getting its value from str_len its an array because of return on $text = str_split($text);

So you have overwritten your $text variable you can fix it by changing order get length first then split.

You overwrite your first variable $text

$count = strlen($text);

In this line $text is an array, because you changed it in the first line of your method.

Try inverting the two first lines:

$count = strlen($text);
$text = str_split($text);

Note

This will solve your secondary problem, and enable your algorithm to run without errors. It doesn't fix your algorithm, but at least you will be able to debug it now.

str_split turns the string into an array.

str_length brings the length of a string which you have overwritten with an array already. you need count()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM