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 ---!>';
    }
}
2
  • You're probably passing in a string as the $array parameter. Check the calling code and see what arguments you're passing in Commented Apr 28, 2013 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! Commented Apr 28, 2013 at 19:57

1 Answer 1

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 ---!>';
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. I have solved problem with this function: paste2.org/pXkLwDGI

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.