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

时间:2017-11-23 10:25:48

标签: 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 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>";

结果:

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
)

2 个答案:

答案 0 :(得分:3)

让我们用一个更短的字符串解释一下。 $text = "Message=Great school. Great teaching team.";

当你像这样爆炸那个字符串时:$tempArray = explode("Message=", $text);然后它会找到一个“Message =”。当您在一点分割某些东西时,您将不得不在之后分割。在这种情况下,第一部分是“Message =”之前的部分,第二部分是“Message =”之后的部分。但现在因为事件发生在字符串的开头,第一部分将是一个空字符串。

如果你的$文字是这样的:
$text = "FooBar Message=Great school. Great teaching team."; 然后你仍然会有2件,但第一件不会是空的,但会包含“Foobar”。

答案 1 :(得分:0)

使用 array_filter()它会过滤你的数组

$tempArray = array_filter(explode("Message=", $text));
  echo "<pre>";
 foreach($tempArray as $value){
   print_r(explode("Author=", $value));
}
echo "</pre>"; 

有关array_filter()的更多信息: - http://php.net/manual/en/function.array-filter.php