What am I doing wrong with this? If I just run this:
$region = EM_Locations::get(array('orderby'=>'region_name'));
all is good. However when I add in the array_unique:
$region = EM_Locations::get(array('orderby'=>'region_name'));
$reg = array_unique($region)
It breaks and get "EM_Location could not be converted to string"
array_unique() sorts the values treated as string, two elements are
considered equal if and only if (string) $elem1 === (string) $elem2.
You can add __toString() method for the EM_Location class.
From the documentation for array_unique:
array_unique() sorts the values treated as string at first, then will
keep the first key encountered for every value, and ignore all
following keys.
This means that the values must be converted to a string for comparison, but your values appear to be of type EM_Location, which PHP can't figure out how to convert to a string.
Related
I am dealing with very strange issue of dealing with garbage in iterating through variable which has been cast to an array
$arr = (array)$var; // problem
$arr = array($var); // ok
The first method seems to work fine on values with integers, but not with strings. Is there any documented difference and does php have real casting ?
The problem is with lavarel 4, Database sources, function on line 704
If $var is a scalar, it's documented that both lines do the same:
For any of the types: integer, float, string, boolean and resource, converting a value to an array results in an array with a single element with index zero and the value of the scalar which was converted. In other words, (array)$scalarValue is exactly the same as array($scalarValue).
http://www.php.net/manual/en/language.types.array.php#language.types.array.casting
There are two ways to cast a variable in PHP as a specific type.
using the settype() function
using (int) (bool) (float) etc
More Info : http://www.electrictoolbox.com/type-casting-php/
Parsing a string value to a float value is not consistent in my PHP project. Altough I'm not changing my code and the values to parse are always the same I sometimes get a result with a comma and sometimes with a point.
The incoming value is for example: 35,59
Before parsing this value I first replace the comma by a point.
$value = '35,59';
$value = (float)str_replace(',', '.', $value);
var_dump($value);
When I now use this value in my insert query, this sometimes results in a bug because a comma is used.
This is all a bit weird to me, has anyone experienced this before? How can I prevent this from happening?
Edit:
I indeed forgot my quotes in this example, but I did use quotes in my code
$value = 35,59;
The problem is that the parser cannot recognize "," as a decimal delimiter. Your locale may define numbers this way but programmatically you must use periods or declare the value as a string.
$value = "35,59";
// or
$value = 35.59;
If you get 35,59, that must be hard-coded since any data-source returning this value is automatically treated as a string.
See http://us2.php.net/numberformatter.parse for information on how to cast the formatted string correctly.
I.e
$fmt = new NumberFormatter( 'sv_SE', NumberFormatter::DECIMAL );
$value = "35,59";
echo $fmt->parse($value); // 35.59
Edit
Also your str_replace will fail for X number of locales since some usually use "," as a thousand separator.
I wanted to check for matching values in multiple arrays, so I made a multi-dimensional array by pushing them into $array and then wrote this line of code:
$result = call_user_func_array('array_intersect', $array);
I am getting the result I want, but I am always getting this notice on that particular line of code:
Notice: Array to string conversion
Wondering what's causing this. Hope someone can enlighten me.
Your arrays (the first-level items inside $array) themselves contain arrays. This is unsupported by array_intersect, because it treats the array items as strings for purposes of determining equality:
Note: Two elements are considered equal if and only if (string) $elem1 === (string) $elem2.
In words: when the string representation is the same.
I can't say definitely without knowing what exactly you are trying to do, but a possible solution is to use array_uintersect instead which will allow you to specify in code how to compare items without necessarily casting them to string.
$test['test'] = 'test';
if(isset($test['test']['x']))
return $test['test']['x'];
This statement returns the first character of the string in $test['test'] (in this case 't'), no matter what is specified as dimension 2.
I can't wrap my head around this behavior. I use isset() all the time. Please advise.
This happens because you're not indexing an array, you're indexing a string. Strings are not arrays in PHP. They happen to share a concept of indexes with arrays, but are really character sequences even though there is no distinct char data type in PHP.
In this case, since strings are only indexed numerically, 'x' is being converted into an integer, which results in 0. So PHP is looking for $test['test'][0]. Additionally $test is only a single-dimensional array, assuming 'test' is the only key inside.
Not really relevant to your question, but if you try something like this you should get 'e', because when converting '1x' to an integer, PHP drops anything that isn't a digit and everything after it:
// This actually returns $test['test'][1]
return $test['test']['1x'];
If you're looking for a second dimension of the $test array, $test['test'] itself needs to be an array. This will work as expected:
$test['test'] = array('x' => 'test');
if (isset($test['test']['x']))
return $test['test']['x'];
Of course, if your array potentially contains NULL values, or you want to make sure you're checking an array, use array_key_exists() instead of isset() as sirlancelot suggests. It's sliiiiightly slower, but doesn't trip on NULL values or other indexable types such as strings and objects.
Use array_key_exists for testing array keys.
It's returning 't' because all strings can be treated as arrays and 'x' will evaluate to 0 which is the first letter/value in the variable.
I'm trying to create an associative array like this:
$key = '0'
$arr = array((string)$key=>$value);
Later, checking is_string(array_keys($arr)[0]) returns false.
The casting didn't help, using " instead of ' didn't help.
Am I doing something wrong, or is there another way around this, or is it impossible to have a numeric string array key?
In PHP, strings are converted to numbers when used as index, if they are purely numeric. When assigning it as an array key, it is converted to an integer, and same on access, you can use $arr['0'] to access the key 0.
PHP handles indexes of arrays in a bit more special way than just assigning to variables. Rules are clearly written in manual. Here is excerpt regarding your question.
A key may be either an integer or a string. If a key is the standard representation of an integer, it will be interpreted as such (i.e. "8" will be interpreted as 8, while "08" will be interpreted as "08"). Floats in key are truncated to integer. The indexed and associative array types are the same type in PHP, which can both contain integer and string indices.
Quote from
http://php.net/manual/en/language.types.array.php
Set as Integer, but Accessible as String...
It appears that the keys will be assined the type of "integer" unless something about their value prevents the assignment. You are able to access them as strings, as I demonstrate with the gettype() line.
$array = array("0" => "Jonathan", "1" => "Sampson");
$keys = array_keys($array);
print gettype((string)$keys[0]); // string
when assigning values to a variable that could possibly be interpreted as multiple different types NEVER rely on them to actually be a specific type.