Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/260.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 新分块的、不同字符串长度的元素数组中缺少某些数组元素_Php_Arrays_Chunks - Fatal编程技术网

Php 新分块的、不同字符串长度的元素数组中缺少某些数组元素

Php 新分块的、不同字符串长度的元素数组中缺少某些数组元素,php,arrays,chunks,Php,Arrays,Chunks,我试图将一个字符串数组转换为新的字符串数组,通过相应地添加同级项来更改每个元素的字数。但我遇到的问题是,以前数组的某些部分没有按要求进行转换 这是到目前为止我的代码: $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,

我试图将一个字符串数组转换为新的字符串数组,通过相应地添加同级项来更改每个元素的字数。但我遇到的问题是,以前数组的某些部分没有按要求进行转换

这是到目前为止我的代码:

$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);
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 

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!

我的预期结果是:

$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);
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 

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!


任何帮助都将不胜感激。

如果您试图将
$text\u array
的元素重新组织为不同的字长,最简单的解决方案是创建一个包含所有字词的数组(将现有字符串重新组合成一个,然后再次拆分)然后使用将其拆分为
n
单词组。例如:

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));
输出:

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!
)
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!',
)

正则表达式提供了一种非常简洁的技术

此一行程序将在每30个单词(非空白子字符串)后分割空白字符上的连接字符串

模式匹配30个“单词”的集合,然后用
\K
忘记它们,然后使用下一个空格作为定界字符。容易做

代码:()

输出:

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!
)
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!',
)

不清楚你想做什么。您正在谈论更改字数和附加兄弟项,但没有解释这一切意味着什么。没有例子,没有指示错误结果与正确结果的对比。你需要更多的细节。试着创建一个最小的可重复的例子。