Array to string conversion and Only variables should be passed by reference - php

I am not experienced with php(i am new).
i am trying to use this code http://mach13.com/how-to-get-a-variable-name-as-a-string-in-php to find the name of a variable, but I keep getting:
Array to string conversion for the line :
$aDiffKeys = array_keys (array_diff_assoc ($aDefinedVars_0, $aDefinedVars));
and also i get "only variables should be passed by reference" when I use
var_name($a, get_defined_vars());
How can I make those messages disappear? Because the entire code is working(I get the desired output).
Here is the code
<?php
function var_name (&$iVar, &$aDefinedVars)
{
foreach ($aDefinedVars as $k=>$v)
$aDefinedVars_0[$k] = $v;
$iVarSave = $iVar;
$iVar =!$iVar;
$aDiffKeys = array_keys (array_diff_assoc ($aDefinedVars_0, $aDefinedVars));
$iVar = $iVarSave;
return $aDiffKeys[0];
}
$a=12;
echo var_name($a,get_defined_vars());
//ini_set('display_errors', '0');
?>

The Array to String conversion notice started in PHP v5.4.0. Since array_diff_assoc() doesn't search recursively, it is notifying you that it found that one of the values in your array is also an array and it had to convert it to a string.
Here's an example on how to use array_diff_assoc() for a multi-dimensional array... http://nl3.php.net/manual/en/function.array-diff-assoc.php#73972
Or perhaps switching out array_diff_assoc() for array_diff_key() would work for your purpose if you are only comparing the keys?

only variables should be passed by reference
You are passing the result of a function call as an argument. You aren't passing a variable.
$vars = get_defined_vars();
echo var_name($a,$vars);
Also, unless you're intentionally modifying one of the variables you shouldn't be passing it as a reference. That way any changes made are local to the function.

Related

Array to string conversion when comparing arrays [duplicate]

I am not experienced with php(i am new).
i am trying to use this code http://mach13.com/how-to-get-a-variable-name-as-a-string-in-php to find the name of a variable, but I keep getting:
Array to string conversion for the line :
$aDiffKeys = array_keys (array_diff_assoc ($aDefinedVars_0, $aDefinedVars));
and also i get "only variables should be passed by reference" when I use
var_name($a, get_defined_vars());
How can I make those messages disappear? Because the entire code is working(I get the desired output).
Here is the code
<?php
function var_name (&$iVar, &$aDefinedVars)
{
foreach ($aDefinedVars as $k=>$v)
$aDefinedVars_0[$k] = $v;
$iVarSave = $iVar;
$iVar =!$iVar;
$aDiffKeys = array_keys (array_diff_assoc ($aDefinedVars_0, $aDefinedVars));
$iVar = $iVarSave;
return $aDiffKeys[0];
}
$a=12;
echo var_name($a,get_defined_vars());
//ini_set('display_errors', '0');
?>
The Array to String conversion notice started in PHP v5.4.0. Since array_diff_assoc() doesn't search recursively, it is notifying you that it found that one of the values in your array is also an array and it had to convert it to a string.
Here's an example on how to use array_diff_assoc() for a multi-dimensional array... http://nl3.php.net/manual/en/function.array-diff-assoc.php#73972
Or perhaps switching out array_diff_assoc() for array_diff_key() would work for your purpose if you are only comparing the keys?
only variables should be passed by reference
You are passing the result of a function call as an argument. You aren't passing a variable.
$vars = get_defined_vars();
echo var_name($a,$vars);
Also, unless you're intentionally modifying one of the variables you shouldn't be passing it as a reference. That way any changes made are local to the function.

Function for reverse order of values in an array php

I'm new to PHP and I was asked to write a function that accepts an array as a parameter and then prints the array in reverse order. Here is what I have so far:
<?php
function RevOrder ($arr1) {
$arr1 == array();
echo array_reverse($arr1);
}
RevOrder (array(1,4,2,5,19,11,28));
?>
It is supposed to output 28, 11, 19, 5, 2, 4, 1 but I keep getting an array to string conversion error.
echo expects a string while you are passing an array, hence the array to string conversion error. It also looks like you are not properly checking if the param passed is an array. Try this:
<?php
function RevOrder($arr1) {
if (is_array($arr1)) {
return array_reverse($arr1);
}
return false;
}
$reversedArray = RevOrder(array(1,4,2,5,19,11,28));
// Option 1
print_r($reversedArray);
// Option 2
echo(implode(', ', $reversedArray));
<?php
function RevOrder (array $arr1) {
echo implode(", ", array_reverse($arr1));
}
RevOrder (array(1,4,2,5,19,11,28));
But note that this isn't particularly good design - your functions should do one thing. In this case you should instead write a function to print an array according to your liking and then pass it reversed array. Although in this case I guess it's ok to have a helper function for printing the array in reversed order but when you're doing something more complicated you should consider this.
EDIT:
You could do something like this:
function printArray(array $arr){
echo implode(", ", $arr);
}
printArray(array_reverse($arr));
As for why you can't just echo array see this
Arrays are always converted to the string "Array"; because of this,
echo and print can not by themselves show the contents of an array. To
view a single element, use a construction such as echo $arr['foo'].
See below for tips on viewing the entire contents.
Also I added type-hints for array so that when you pass something that's not an array you get an error.

array_merge(array($string), $array) or array_merge((array)$string, (array)$array)

Question is related with example from here http://lv1.php.net/array_merge
$beginning = 'foo';
$end = array(1 => 'bar');
$result = array_merge((array)$beginning, (array)$end);
Usually I use such code $result2 = array_merge( array($beginning), $end );
$end is already an array. Why need (array)$end....
Tested and see the same result.
So question. Is array_merge( array($beginning), $end ) correct code?
Seems now understood why it is reasonable to use (array)
For example $var2 = array('test2');
print_r( array($var2) );
would be multidimensional array
but
print_r( (array)$var2 );
would be the same array as initial.
There is a slight difference between array($foo) and (array)$foo, but it won't affect the output.
While array($foo) will try to build an array out of $foo, obviously returning an array, (array)$foo will try to look at$foo like it is an array, hence returning an array. Both have the exact same result if your variable is a good candidate for an array, but (array)$foo may have a stronger semantic aspect since it exposes your intention of using the variable as an array, rather than building an array out of it.
array_merge only accepts parameters of type array (Since PHP 5.0)
Convert all parameters use typecasting, therefore
Add (array) before the variable, it's means convert the data type into array, case it is not array.
Note:
If you can ensure all of the variables which used in array_merge ARE array. You can direct access it, instead of adding the (array).
Yes, it's correct code. If you are sure that the parameter is already an array you don't need the type casting.

Access JSON values in PHP

{"coord":{"lon":73.69,"lat":17.8},"sys":{"message":0.109,"country":"IN","sunrise":1393032482,"sunset":1393074559},"weather":[{"id":800,"main":"Clear","description":"Sky is Clear","icon":"01n"}],"base":"cmc stations","main":{"temp":293.999,"temp_min":293.999,"temp_max":293.999,"pressure":962.38,"sea_level":1025.86,"grnd_level":962.38,"humidity":78},"wind":{"speed":1.15,"deg":275.503},"clouds":{"all":0},"dt":1393077388,"id":1264491,"name":"Mahabaleshwar","cod":200}
I am trying to fetch description from the weather from the json above but getting errors in php. I have tried the below php code:
$jsonDecode = json_decode($contents, true);
$result=array();
foreach($jsonDecode as $data)
{
foreach($data{'weather'} as $data2)
{
echo $data2{'description'};
}
}
Any help is appreciated. I am new in using json.
You have to use square brackets ([]) for accessing array elements, not curly ones ({}).
Thus, your code should be changed to reflect these changes:
foreach($data['weather'] as $data2)
{
echo $data2['description'];
}
Also, your outer foreach loop will cause your code to do something completely different than you intend, you should just do this:
foreach($jsonDecode['weather'] as $data2)
{
echo $data2['description'];
}
Your $jsonDecode seems to be an array, so this should work-
foreach($jsonDecode['weather'] as $data)
{
echo $data['description'];
}
You can access data directly with scopes
$json = '{"coord":{"lon":73.69,"lat":17.8},"sys":{"message":0.109,"country":"IN","sunrise":1393032482,"sunset":1393074559},"weather":[{"id":800,"main":"Clear","description":"Sky is Clear","icon":"01n"}],"base":"cmc stations","main":{"temp":293.999,"temp_min":293.999,"temp_max":293.999,"pressure":962.38,"sea_level":1025.86,"grnd_level":962.38,"humidity":78},"wind":{"speed":1.15,"deg":275.503},"clouds":{"all":0},"dt":1393077388,"id":1264491,"name":"Mahabaleshwar","cod":200}';
$jsonDecode = json_decode($json, true);
echo $jsonDecode['weather'][0]['description'];
//output Sky is Clear
As you can see wheater` is surrounded with scopes so that means it is another array. You can loop throw that array if you have more than one result
foreach($jsonDecode['weather'] as $weather)
{
echo $weather['description'];
}
Live demo
If the result of decode is an array, use:
$data['weather']
If the result is an object, use:
$data->weather
you have to access "weather" with "[]" operator
like this,
$data["weather"]
There is several things worth answering in your question:
Q: What's the difference between json_decode($data) and json_decode($data, true)?
A: The former converts JSON object to a PHP object, the latter creates an associative array: http://uk1.php.net/json_decode
In either case, there is no point on iterating over the result. You probably want to access just the 'weather' field:
$o = json_decode($data) => use $weather = $o->weather
$a = json_decode($data, true) => use $weather = $a['weather']
Once you have the 'weather' field, look carefully what it is:
"weather":[{"id":800,"main":"Clear","description":"Sky is Clear","icon":"01n"}]
It's an array, containing a single object. That means you will either need to iterate over it, or use $clearSky = $weather[0]. In this case, it does not matter which approach of json_decode did you choose => JSON array is always decoded to a PHP (numeric indexed) array.
But, once you get $clearSky, you are accessing the object and it again matters, which approach you chose - use arrow or brackets, similarly to the first step.
So, the correct way to get for exaple the weather description would be either of these:
json_decode($data)->weather[0]->description
json_decode($data, true)['weather'][0]['description']
Note: In the latter case, dereferencing result of the function call is supported only in PHP 5.4 or newer. In PHP 5.3 or older, you have to create a variable.
Note: I also encourage you to always check if the expected fields are actually set in the result, using isset. Otherwise you will try to access undefined field, which raises an error.

In PHP, filter an Array of Associative Arrays

In PHP, are there any inbuilt functions to turn
[{"id":1, "name":"John"}, {"id":2, "name":"Tim"}]
into
[{"id":1}, {"id":2}]
?
I've used JSON to describe the objects above, but that's just a conceptual representation of my array of associative arrays. I don't want to have to loop manually - something short and elegant I can fit on one line would be nice.
One line, using array_map:
$arr = json_decode('[{"id":1, "name":"John"}, {"id":2, "name":"Tim"}]');
$new_arr = array_map(function($el){$ret=array("id"=>$el->id);return $ret;},$arr);
var_dump(json_encode($new_arr));
array_map(function($arr){return $arr[0];}, $array);
This should do it.
Edit As noted by Jonathon Hibbard, you could pass array element by reference, this way you do not to assign result of the function and just use changed old array. The code should then be modified appropriately.
First decode json by json_decode. You will get an array. Then follow this link to remove an index from an associative array. Then again decode it. It should work.
do something like:
$array = json_decode($some_json_string, true);
array_walk($array, function($value, $key) use(&$array) {
if($key == "name") {
unset($array[$key]);
}
});
Edit:
Cthulhu's answer won't get ya there without re-assigning it. Could use it as a reference though (equal to the walk. though if you want to use the map, its a bit better not to reallocate with a brand new array copy and just pass it by reference, then remove the key with an unset within it and move on.)
array_map(function(&$array) { unset($array['name']; }, $array);

Categories