Jump to content

array_key_exists()


ckrudelux

Recommended Posts

For some reason I can't use a variable in this function.. :) anyone who knows how to solve this?

$array = array("tomato" => "red", "apple" => "green");$key = "tomato";if(isset(array_key_exists($key, $array)){//somecode}

Link to comment
Share on other sites

isset() accepts a variable, not a value. If you want to check if a key exists, simply omit the isset() and test against the return value, like:

if(array_key_exists($key, $array)){

(BTW, you also had a parsing error... a missing ")")

Link to comment
Share on other sites

isset() accepts a variable, not a value. If you want to check if a key exists, simply omit the isset() and test against the return value, like:
if(array_key_exists($key, $array)){

(BTW, you also had a parsing error... a missing ")")

Ow.. I didn't attend to write isset in the example sorry..
$array = array("tomato" => "red", "apple" => "green");$key = "tomato";if(array_key_exists($key, $array)){//somecode}

This is the error massage I get.

Warning: array_key_exists() [function.array-key-exists]: The first argument should be either a string or an integer in C:\wamp\www\pagemodule\beta1.1\lib\sys_load.php on line 11
Link to comment
Share on other sites

Even with the warning at hand, does the function work as expected? If so, I suppose the warning comes because the function is not intended to be used in this way - you're supposed to know the key you're looking for, not gather it from somewhere else.

Link to comment
Share on other sites

Even with the warning at hand, does the function work as expected? If so, I suppose the warning comes because the function is not intended to be used in this way - you're supposed to know the key you're looking for, not gather it from somewhere else.
It returns false then it should be true I know this cause I happen to know the answer this time. But if that don't work how should I do it then any suggestions?
Link to comment
Share on other sites

It returns false then it should be true I know this cause I happen to know the answer this time. But if that don't work how should I do it then any suggestions?
I solved it by making a new function... but I hoped I could had solved it some other way.
Link to comment
Share on other sites

I'm surprised that PHP should issue a warning for putting a variable instead of a literal string or number. Are you certain that the $key variable actually held a string when you tested your program?

Link to comment
Share on other sites

There's nothing wrong with using variables instead of literal values, there's no difference at all to most functions (once inside the function, the value is always a variable). There's nothing wrong with the example code you posted. Maybe you have the code in a loop or something where $key is getting set to something else. You can always use var_dump to see exactly what the value is, including the type.

Link to comment
Share on other sites

There's nothing wrong with using variables instead of literal values, there's no difference at all to most functions (once inside the function, the value is always a variable). There's nothing wrong with the example code you posted. Maybe you have the code in a loop or something where $key is getting set to something else. You can always use var_dump to see exactly what the value is, including the type.
I tested this://returns 1echo array_key_exists($type, $this->data);//Returns truevar_dump(array_key_exists($type, $this->data);//dont workif(array_key_exists($type, $this->data)){}
Link to comment
Share on other sites

.. so you're saying that this returns true:array_key_exists($type, $this->data)but this doesn't work:array_key_exists($type, $this->data)What's the difference?We're missing a lot of information here about context. Context is everything, it's the difference between a line of code working or not. In your context that line may not work because of something else at another point in the code, but it has nothing to do with the value being literal vs. a variable, or the presence of an if statement, or whatever else, none of those things matter. The reason you're getting an error is because of a piece of code that you haven't shown. The basic examples you're giving all work fine. This code (and only this code, no other code on the page):

<?php$array = array("tomato" => "red", "apple" => "green");$key = "tomato";if(array_key_exists($key, $array)){  echo 'exists';}?>

will never produce this error message:Warning: array_key_exists() [function.array-key-exists]: The first argument should be either a string or an integerThe reason it will never produce that message is because it's plainly obvious that $key is in fact a string, because it was declared in the previous line.But that's not the code you're running. Your scripts have other code on them, but you're not showing that code, and that's the code that is causing the problem. At some point the variable $type is being set to something other than a string.

Link to comment
Share on other sites

here is the function:

public function exicute($type){ //$type == description string(11)	//var_dump(array_key_exists($type, $this->data));	This line returns true.	//exit;	if(array_key_exists($type, $this->data)){	 // This line returns false.		$value = $this->this_key_value($type, $this->data);		if($value == "array"){			if(array_key_exists("file", $value)){				include $value['file'];			}else{				return "No data found!";			}		}else{			return $value;		}	}else{		return "Key don't exists!";	}}

Then I add if(){ around it I get the error but not then I just have it standing alone on one line.

Link to comment
Share on other sites

Then the function should work. You're saying you see it print that $type is a string, and then you get an error telling you that $type needs to be a string?
Yes... and if as you say what my functions is working maybe it's the variable I'm sending in to the function.. cause that one returns this with var_dump() object(SimpleXMLElement)#22 (1) { [0]=> string(5) "title"}but then echo it returns title...
Link to comment
Share on other sites

Yes, that's the problem. You're sending a SimpleXMLElement object, not a string. In that case you should be using $type->0 I believe.
So how would I do this then cause what I'm trying to do here is saving the value in variable with this line:$type = $key->attributes()->type;$sys_load->exicute($type);
Link to comment
Share on other sites

Since SimpleXMLElement apparently has a __toString method, which is why echo shows a certain value instead of the word "Object", then you can use the strval function to convert the object to a string. Also, execute doesn't have an "i" in it.

Link to comment
Share on other sites

Since SimpleXMLElement apparently has a __toString method, which is why echo shows a certain value instead of the word "Object", then you can use the strval function to convert the object to a string. Also, execute doesn't have an "i" in it.
My spelling is just as good as usually I see :) Not totally sure how I should use the __toString method.. php.net __toString();
Link to comment
Share on other sites

You don't need to use __toString directly, any class that implements a method called __toString will have that method called when the object needs to be converted to a string. When you use echo, for example, it only uses a string, so when you send an object to echo it will try to find a __toString method and call that if it exists. If there's no __toString method on the object, it will just convert to the string "Object".All of that is done automatically though, to actually convert you just use the strval function.

Link to comment
Share on other sites

You don't need to use __toString directly, any class that implements a method called __toString will have that method called when the object needs to be converted to a string. When you use echo, for example, it only uses a string, so when you send an object to echo it will try to find a __toString method and call that if it exists. If there's no __toString method on the object, it will just convert to the string "Object".All of that is done automatically though, to actually convert you just use the strval function.
Like this? I don't really get the logic of this method so I'm just guessing here.public function __toString(){ return strval($this);}
Link to comment
Share on other sites

I was just trying to explain how strval and converting to a string works. PHP doesn't know how to convert any random object to a string, so when you try to convert an object to a string it looks for a method called __toString defined on the object. The SimpleXMLElement class has implemented __toString, which is why it converts to the value you're looking for instead of the string "Object".

Link to comment
Share on other sites

I was just trying to explain how strval and converting to a string works. PHP doesn't know how to convert any random object to a string, so when you try to convert an object to a string it looks for a method called __toString defined on the object. The SimpleXMLElement class has implemented __toString, which is why it converts to the value you're looking for instead of the string "Object".
okay then I think I get it :)
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...