Pregunta

I have an array and some of the values have the text value "unknown".

If I use strcmp, I'm not getting the results as expected.

For example:

echo $array[8];

outputs the word "unknown".

strcmp($array[8],"unknown");

outputs -104.

strcmp(trim($array[8]),"unknown");

Outputs -57.

I don't understand why these strings are not equal. I'm just trying to get a 0 value so I can filter out the array values with "unknown" with a loop.

¿Fue útil?

Solución 2

If you want to check if string is equal, use ===

if(trim($array[8]) === "unknown"){ ..

You can even write your own callback for the filter method:

$array_filtered = array_filter($array, function($value){
    return trim($value) !== "unknown";
});

Otros consejos

You re wrongly trimming it up..

The right way...

<?php
$arr[8]='unknown';
echo strcmp(trim($arr[8]),"unknown"); //"prints" 0

Demo

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top