0

I have an error with my function.

Notice: Array to string conversion in....

Just I wanna check array availability & return if have. I have searched but, I can't do.

Here is my function:

function if_array_exists($array,$value) {
    if(isset($array[$value]) || array_key_exists($value,$array)) {
        $result=array();
        $result[0]=$value;
        return $result;
    } else {
        return '<!--- no array named as '.$value.' value in ---!>';
    }
}
liveth19937
  • 61
  • 1
  • 2
  • 6
  • You're probably passing in a string as the `$array` parameter. Check the calling code and see what arguments you're passing in – Bojangles Apr 28 '13 at 19:53
  • 4
    debug your code. start with looking at the line that is causing the error. then check what you are passing to the function etc, as @Bojangles suggested! – michi Apr 28 '13 at 19:57

1 Answers1

2

If you check to see if it's an actual array first, that should prevent the notice (because it will skip directly to the else.

function if_array_exists($array,$value) {
    if(is_array($array) && (isset($array[$value]) || array_key_exists($value,$array))) {
        $result=array();
        $result[0]=$value;
        return $result;
     } else {
        return '<!--- no array named as '.$value.' value in ---!>';
    }
}
jerdiggity
  • 3,655
  • 1
  • 29
  • 41