Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Empty value in array as first item (PHP)

Tags:

arrays

php

I'm trying to create an array out text based on splitting the text up on Message= and Author. However it's return an empty value as the first array item. I'm can't get my head round why this is happening. So i've decided to come here for some advise.

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

Result:

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
)
like image 539
YaBCK Avatar asked Jun 14 '26 14:06

YaBCK


1 Answers

Lets explain this with a shorter String. $text = "Message=Great school. Great teaching team.";

When you explode that string like this: $tempArray = explode("Message=", $text); then it finds one occurrence of "Message=". When you split something at one point, you will have to pieces afterwards. In this case the first piece is what was standing before "Message=", the second piece is what was after "Message=". But now because the occurrence was at the start of the string, the first piece will be an empty string.

If your $text would be like this:
$text = "FooBar Message=Great school. Great teaching team."; then you would still have 2 pieces, but the first one would not be empty but would contain "Foobar ".

like image 173
kscherrer Avatar answered Jun 16 '26 17:06

kscherrer



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!