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
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.
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.
Is it possible in php to change the name used to create an associative array? I am using mongo in php but it's getting confusing using array() in both cases of indexed arrays and associative arrays. I know you can do it in javascript by stealing the Array.prototype methods but can it be done in php my extending the native object? it would be much easier if it was array() and assoc() they would both create the same thing though.
EDIT -------
following Tristan's lead, I made this simple function to easily
write in json in php. It will even take on variable from within
your php as the whole thing is enclosed in quotes.
$one = 'newOne';
$json = "{
'$one': 1,
'two': 2
}";
// doesn't work as json_decode expects quotes.
print_r(json_decode($json));
// this does work as it replaces all the single quotes before
// using json decode.
print_r(jsonToArray($json));
function jsonToArray($str){
return json_decode(preg_replace('/\'/', '"', $str), true);
}
In PHP there is no "name used to create an associative array" or "name used to create an indexed array". PHP Arrays are ordered maps like in many other scripting languages.
This means that you can use an array whichever way you please.
If you wanted an indexed array..
$indexedArray = array();
$indexedArray[] = 4; // Append a value to the array.
echo $indexedArray[0]; // Access the value at the 0th index.
Or even..
$indexedArray = [0, 10, 12, 8];
echo $indexedArray[3]; // Outputs 8.
If you want to use non integer keys with your array, you simply specify them.
$assocArray = ['foo' => 'bar'];
echo $assocArray['foo']; // Outputs bar.
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.
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);