G
G
gfan2014-01-11 20:10:51
PHP
gfan, 2014-01-11 20:10:51

How to check the contents of an array in PHP?

There is an array $answer1[0], in it something like HTTP/1.1 302 FOUND.
I need to check if the array has HTTP/1.1 302 FOUND - perform an action.
if($answer1[0]=="HTTP/1.1 302 FOUND") doesn't work for unknown reasons, I'd like to know why.
print_r($answer1[0]) will print HTTP/1.1 302 FOUND.
I tried this:
strpos($answer1[0], "HTTP/1.1 302 FOUND")) - this option will display the position of the first match, that is, 0. 0 in if will not be executed) What is better to use in this case?

Answer the question

In order to leave comments, you need to log in

4 answer(s)
E
egor_nullptr, 2014-01-11
@gfan

Try if (trim($answer1[0]) == "HTTP/1.1 302 FOUND"). If you want with strpos, then

if (strpos($answer1[0], "HTTP/1.1 302 FOUND") !== false)
.

D
DrNemo, 2014-01-11
@DrNemo

if($answer1[0]=="HTTP/1.1 302 FOUND")
doesn't work most likely because of trailing spaces, try this:
if(trim($answer1[0])=="HTTP/1.1 302 FOUND" )
with strpos:
if(strpos($answer1[0], "HTTP/1.1 302 FOUND")) !== false)
Ps:
here's one for you:
$answer1 = array_map('trim', $answer1); // this is if you have spaces on the sides
if(array_search("HTTP/1.1 302 FOUND", $answer1) !== false)

V
Vladislav Ivasik, 2014-01-11
@iqw

See also other string functions.
www.softtime.ru/bookphp/gl3_3.php
I'm sure you'll find a lot of interesting things there. Of course, the way is cooler - so preg_match with a regular expression, but if this is difficult to do yet, use string functions.

2
2byte, 2014-01-11
@2byte

Can

if (trim(md5($answer1[0])) == md5("HTTP/1.1 302 FOUND"))

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question