Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/294.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 - Fatal编程技术网

数组中的空值作为第一项(PHP)

数组中的空值作为第一项(PHP),php,arrays,Php,Arrays,我试图在Message=和Author上拆分文本,然后创建一个文本数组。但是,它将返回一个空值作为第一个数组项。我不明白为什么会发生这种事。所以我决定来这里寻求一些建议 $text = "Message=Great school. Great teaching team. School does an amazing job at supporting ALL my child’s needs. They have always gone the extra mile for him. So t

我试图在
Message=
Author
上拆分文本,然后创建一个文本数组。但是,它将返回一个空值作为第一个数组项。我不明白为什么会发生这种事。所以我决定来这里寻求一些建议

$text = "Message=Great school. Great teaching team. School does an amazing job at supporting ALL my child’s needs. They have always gone the extra mile for him. So thank you all. You do a great job! Author=Parent

Message=Fabulous school with a friendly atmosphere. Staff and the Headteacher are approachable. I cannot praise the school highly enough. Author=Parent

Message=For the past 9 years, through 2 children, I have always felt NPS goes the extra mile to support it's students in all areas of the curriculum. The staff are fantastic, hard-working and very approachable. It is a great school and I am glad we chose it all those years ago. Author=Parent

Message=This school has always been a positive influence on my children (2 of which didn't want to leave!) I have recommended this school to several people in the past and will continue to do so in the future. High praise for all the teachers and assistants who make the school what it is! Author=Parent

Message=Great team of staff at NPS. Ladies at reception are so helpful. Newsletter and Facebook page are very informative and helpful. Breakfast club - excellent - my child raves about it. Love the Friday celebration assembly. The sense of pride and encouragement felt is overwhelming - a wonderful celebration of the children. Author=Parent

Message=Thanks for the beautiful memories, good times and great learning experiences for Adam, Faith and Rose! Truly has been a life time adventure for the kids...one that will last a lifetime of memories! Hope to visit Neston again in the future! Parent=A parent, after family returned home overseas

Message=We would like to thank all the teachers who made the Delamere trip possible, Kieran had an amazing time and is already asking when he can go again Author=Parent, following Year 2 residential visit";

$tempArray = explode("Message=", $text);
echo "<pre>";
foreach($tempArray as $value){
   print_r(explode("Author=", $value));
}
echo "</pre>";
使用数组\u filter()它将过滤您的数组

$tempArray=array_过滤器(分解(“Message=”,$text));
回声“;
foreach($tempArray作为$value){
打印(分解(“Author=”,$value));
}
回声“;

有关array_filter()的详细信息:-

让我们用一个较短的字符串来解释这一点。
$text=“Message=很棒的学校,很棒的教学团队。”

当您像这样分解该字符串时:
$tempArray=explode(“Message=”,$text)然后找到一个出现的“Message=”。当你在某一点上把某物劈开时,你将不得不在以后把它撕成碎片。在本例中,第一块是“Message=”之前的内容,第二块是“Message=”之后的内容。但是现在,因为事件发生在字符串的开头,所以第一个片段将是一个空字符串

如果您的$text如下所示:
$text=“FooBar Message=很棒的学校,很棒的教学团队。”

然后仍然有两个片段,但第一个片段不是空的,而是包含“Foobar”。

使用array_filter()
explode
将给定分隔符周围的字符串分隔开-如果字符串以分隔符开头(或结尾),则会得到一个空字符串作为结果的一部分。这是将其用于文本解析(而不是正则表达式等)的(许多)缺点之一。您可以使用
array\u filter
,或者简单的
if…
检查结果以解决此问题。
Array
(
    [0] => 
)
Array
(
    [0] => Great school. Great teaching team. School does an amazing job at supporting ALL my child’s needs. They have always gone the extra mile for him. So thank you all. You do a great job! 
    [1] => Parent


)
Array
(
    [0] => Fabulous school with a friendly atmosphere. Staff and the Headteacher are approachable. I cannot praise the school highly enough. 
    [1] => Parent


)
Array
(
    [0] => For the past 9 years, through 2 children, I have always felt NPS goes the extra mile to support it's students in all areas of the curriculum. The staff are fantastic, hard-working and very approachable. It is a great school and I am glad we chose it all those years ago. 
    [1] => Parent


)
Array
(
    [0] => This school has always been a positive influence on my children (2 of which didn't want to leave!) I have recommended this school to several people in the past and will continue to do so in the future. High praise for all the teachers and assistants who make the school what it is! 
    [1] => Parent


)
Array
(
    [0] => Great team of staff at NPS. Ladies at reception are so helpful. Newsletter and Facebook page are very informative and helpful. Breakfast club - excellent - my child raves about it. Love the Friday celebration assembly. The sense of pride and encouragement felt is overwhelming - a wonderful celebration of the children. 
    [1] => Parent


)
Array
(
    [0] => Thanks for the beautiful memories, good times and great learning experiences for Adam, Faith and Rose! Truly has been a life time adventure for the kids...one that will last a lifetime of memories! Hope to visit Neston again in the future! 
    [1] => A parent, after family returned home overseas


)
Array
(
    [0] => We would like to thank all the teachers who made the Delamere trip possible, Kieran had an amazing time and is already asking when he can go again 
    [1] => Parent, following Year 2 residential visit
)
$tempArray = array_filter(explode("Message=", $text));
  echo "<pre>";
 foreach($tempArray as $value){
   print_r(explode("Author=", $value));
}
echo "</pre>";