0

I have an issue with an array being returned from an API request which i want to search for a specific value and perform a certain operation for but i have been unsucessful in making it work. I have used array_search() function and done several things based on reading similar questions here and online but still no luck. Anyone know what i am doing wrong?

$responses = explode("\n", file_get_contents($url));
print_r($responses);

print_r($responses) displays:

Array
(
[0] => destination_msisdn=233887
[1] => country=
[2] => countryid=
[3] => operator=
[4] => operatorid=
[5] => authentication_key=XXXXXX
[6] => error_code=101
[7] => error_txt=Destination MSISDN out of range
)

and this is what i currently have in my code for array search

if(array_search("error_code=101", $responses)){
   echo "Incorrect!";
 }

The result i get from:

   print_r(file_get_contents($url));

is

 destination_msisdn=23345678 country= countryid= operator= operatorid= authentication_key=XXXXX error_code=101 error_txt=Destination MSISDN out of range

Please I am not trying to explode any array value I am trying to search for a string in the array and perform an operation as per my if statement.

Thanks

Unicornese
  • 135
  • 2
  • 15
  • Could you add the result of `print_r(file_get_contents($url));` into your question? – KhorneHoly Dec 03 '15 at 14:29
  • What is the problem/question ? – Rizier123 Dec 03 '15 at 14:42
  • @Rizier123 the problem is the string i want to echo in the if statement is not echoed – Unicornese Dec 03 '15 at 14:46
  • @uzeeOnCodes Do: `var_dump($responses);` and check if the value `error_code=101` is really in your array. Also check for `array_search("error_code=101", $responses) !== FALSE` since the key might be 0. – Rizier123 Dec 03 '15 at 14:48
  • Possible duplicate of [Explode a string to associative array](http://stackoverflow.com/questions/14133780/explode-a-string-to-associative-array) – hdost Dec 03 '15 at 14:49
  • 1
    @hdost I am not tryiing to explode a string to an associative array I want to check if a string value is returned in the array and perform an operation if condition is true – Unicornese Dec 03 '15 at 14:52
  • @uzeeOnCodes Is this your actual file without new lines? Also what is the result from my above comment? – Rizier123 Dec 03 '15 at 14:54
  • @uzeeOnCodes if you explode it into an associative array there's no need to do a search , you could then just access $responses["error_code"] and check if it equals 101 – hdost Dec 03 '15 at 14:57
  • @Rizier123 `var_dump($responses);` shows up `array (size=9) 0 => string 'destination_msisdn=23356789 ' (length=28) 1 => string 'country= ' (length=9) 2 => string 'countryid= ' (length=11) 3 => string 'operator= ' (length=10) 4 => string 'operatorid= ' (length=12) 5 => string 'authentication_key=1449154654 ' (length=30) 6 => string 'error_code=101 ' (length=15) 7 => string 'error_txt=Destination MSISDN out of range ' (length=42) 8 => string '' (length=0)` – Unicornese Dec 03 '15 at 14:58
  • @uzeeOnCodes Do you see that: `(length=30)` and `'error_code=101 '` this means you probably have some spaces or new lines at the end of your line. Use: `$responses = array_map("trim", $responses);` to remove them. – Rizier123 Dec 03 '15 at 15:00
  • @Rizier123 thanks man. You are right there were some silly spaces somewhere cos your solution worked. Can you put it as an answer so i can mark it as accepted? – Unicornese Dec 03 '15 at 15:11

2 Answers2

2

As we found out in the comments your value error_code=101 is 30 characters long and probably has some spaces at the end, so that's why it doesn't match.

Now you either can remove these spaces when you explode it into an array, e.g.

$responses = preg_split("/\s*\n\s*/", file_get_contents($url));

Or you remove them for each element, e.g.

$responses = array_map("trim", $responses);

Also I would recommend you to check for:

array_search("error_code=101", $responses) !== FALSE

Since the key of the value also could be 0, which would result in not entering the if statement.

Rizier123
  • 58,877
  • 16
  • 101
  • 156
-1

starting from the fact your code works for me.

Can you try instead exploding the array, searching on the string like this?

if(strpos(file_get_contents($url),"error_code=101")  !== FALSE)
    echo "Incorrect!";

[edited]

Alessandro.Vegna
  • 1,262
  • 10
  • 19