1

I have the following code

$checkarray = unserialize(file_get_contents('serialized.txt'));

var_dump($checkarray);
foreach($checkarray as $_index => $_image )
{
    echo strval($_index)." = ".var_dump($_image)."<br>";
}
var_dump(array_search('pirates_of_love_and_kingdoms.jpg',$checkarray));
var_dump(in_array('pirates_of_love_and_kingdoms.jpg',$checkarray));

the contents of 'serialized.txt' can be found here (http://textuploader.com link, not a download link, will need to copy and paste into new file if you want to use it)

the first var_dump output the array however because i have xdebug not the entire array is outputted, i'm not looking to get this fixed, it just confirms that the file was imported and unserialize correctly, the loop outputs everything in the array confirming that every value is a string (thanks to xdebug), the final 2 var_dumps are to output the results of the functions.

when i run my code, both var_dumps output false, however if i use the browser to search for the text i do find it so i know it's in the array.

I know that array_search returns the key in the array if the needle is found while in_array returns true if the needle is found and both will return false if the needle can't be found, however i do not get how neither can find it when i can confirm it's outputted in the loop and in my serialized.txt file at the same index as what is specified in the file.

i have already checked the basics, white spaces, new lines, casing in both what is outputted on the screen and in the file, can anyone explain to me what i have done wrong?

2
  • Check for whitespace, newlines, case sensitivity etc. You say the string is in your file - those functions are searching for it as an array entry with nothing else involved in it. Also, use file() to read your text file into an array instead of file_get_contents() Commented Jan 13, 2014 at 22:23
  • @scrowler i'm using file_get_contents() because it gets it as a string and not an array, if you looked in the text file i uploaded you'll see that it's a serialized string, what i search for in the 2 functions is identical to what i search for using the browser and in notepad++ Commented Jan 13, 2014 at 22:32

1 Answer 1

1

The line breaks at the end of each line are causing problems for you. Do a trim inside your foreach:

$checkarray = unserialize(file_get_contents('serialized.txt'));

var_dump($checkarray);
foreach($checkarray as $_index => $_image )
{
    $checkarray[$_index] = trim($_image);
    echo strval($_index)." = ".var_dump($_image)."<br>";
}
var_dump(array_search('pirates_of_love_and_kingdoms.jpg',$checkarray));
var_dump(in_array('pirates_of_love_and_kingdoms.jpg',$checkarray));
Sign up to request clarification or add additional context in comments.

1 Comment

thanks, strange though, as i said i used notepad++ to search for '\n' using the extended search and it returned nothing, is there like another invisible line break value?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.