Search code examples
phparrayschunks

Some array elements missing from newly chunked, different string length element array


I am trying to convert a string array into new string array, changing the word count of each elements by appending sibling items accordingly. But the problem I am having is some part of previous array is not converting as required.

Here is my code so far :

$text_array = ['He needs to cultivate in order', 
'to be at the fourth level of the', 
'Martial Body Stage. Does he have inner energy?"', 
'Everyone jeered, laughed, and taunted.', 
'Qin Yun turned deaf ear to their taunts.',  
'His eyes were filled with sincerity as he',  
'looked at Yang Shiyue and said, "Teacher,', 
'I only formed my elemental energy this morning.', 
'I still not familiar with the control of', 
'my elemental energy and inner energy."',  
'After the empress heard the jeers from the',  
'crowd, she let out a sigh of relief and',  
'sneered, "This is only a little bit of',  
'inner Qi that you forced out.', 
'You have not yet stepped',  
'into the fourth level',  
'of the Martial Body realm and have no',  
'chance of breaking through. embarrass yourself!'];

        $last_converted_index = 0;
        $new_string_array = [];
        $single_valid_length_string = '';
        foreach (array_slice($text_array, $last_converted_index) as $item) {

            if (str_word_count($single_valid_length_string . $item) < 30) {

                $single_valid_length_string .= $item . ' ';
                $last_converted_index++;

            } else {
                $new_string_array[] = $single_valid_length_string."<br><br>";
                $single_valid_length_string = '';
            }

        }

        echo implode($new_string_array);

Output I am getting at the moment is:

He needs to cultivate in order to be at the fourth level of the Martial Body Stage. Does he have inner energy?" Everyone jeered, laughed, and taunted.

His eyes were filled with sincerity as he looked at Yang Shiyue and said, "Teacher, I only formed my elemental energy this morning.

my elemental energy and inner energy." After the empress heard the jeers from the crowd, she let out a sigh of relief and 

My expected result will be:

He needs to cultivate in order to be at the fourth level of the Martial Body Stage. Does he have inner energy?" Everyone jeered, laughed, and taunted.

His eyes were filled with sincerity as he looked at Yang Shiyue and said, "Teacher, I only formed my elemental energy this morning.

my elemental energy and inner energy." After the empress heard the jeers from the crowd, she let out a sigh of relief and 

sneered, "This is only a little bit of inner Qi that you forced out.You have not yet stepped into the fourth level

of the Martial Body realm and have no chance of breaking through. embarrass yourself!

Any help would be appreciated.


Solution

  • If you're trying to reorganise the elements of $text_array to be of a different word length, the simplest solution is to create an array of all the words (by putting the existing strings back together into one and then splitting it again) and then use array_chunk to split that into groups of n words. For example:

    function change_words_length($text, $numwords) {
        $words = explode(' ', implode(' ', $text));
        $output = array();
        foreach (array_chunk($words, $numwords) as $array) {
            $output[] = implode(' ', $array);
        }
        return $output;
    }
    
    print_r(change_words_length($text_array, 10));
    print_r(change_words_length($text_array, 30));
    

    Output:

    Array
    (
        [0] => He needs to cultivate in order to be at the
        [1] => fourth level of the Martial Body Stage. Does he have
        [2] => inner energy?" Everyone jeered, laughed, and taunted. Qin Yun turned
        [3] => deaf ear to their taunts. His eyes were filled with
        [4] => sincerity as he looked at Yang Shiyue and said, "Teacher,
        [5] => I only formed my elemental energy this morning. I still
        [6] => not familiar with the control of my elemental energy and
        [7] => inner energy." After the empress heard the jeers from the
        [8] => crowd, she let out a sigh of relief and sneered,
        [9] => "This is only a little bit of inner Qi that
        [10] => you forced out. You have not yet stepped into the
        [11] => fourth level of the Martial Body realm and have no
        [12] => chance of breaking through. embarrass yourself!
    )
    Array
    (
        [0] => He needs to cultivate in order to be at the fourth level of the Martial Body Stage. Does he have inner energy?" Everyone jeered, laughed, and taunted. Qin Yun turned
        [1] => deaf ear to their taunts. His eyes were filled with sincerity as he looked at Yang Shiyue and said, "Teacher, I only formed my elemental energy this morning. I still
        [2] => not familiar with the control of my elemental energy and inner energy." After the empress heard the jeers from the crowd, she let out a sigh of relief and sneered,
        [3] => "This is only a little bit of inner Qi that you forced out. You have not yet stepped into the fourth level of the Martial Body realm and have no
        [4] => chance of breaking through. embarrass yourself!
    )
    

    Demo on 3v4l.org