PHP Illegal string offset - php

I have a problem with this code :
echo $f[0]['id']; // result 19275
But after the error message is :
Illegal string offset 'id'
Why this error message? Offset 'id' exists and has a value

The reason you would see this error message is because $f[0] returns a string, not a sub array. A var_dump should return the proper answer to your problem. Once your question is updated with the result of that var_dump, I can help you further (print_r will also work)
For example, you could have the following structure
array(
'19275'
)
This satisfies your requirement above, and is what you are most likely being returned.
However, you're assuming the following
array(
array('id' => '19275')
)
You are expecting an associative array, when you are merely dealing with a simple numerically indexed array.

Related

Undefined offset in array - But I know it's in there

I have an array (or associative array), with keys and values.
Then, when trying to read one of the keys, which IS in the array, I get an "undefined offset" notice.
I have two examples where it happened. One was "exploding" a string like "AAA|BBB|CCC", using | as the separator, then trying to read the resulting array at position 1.
var_dump() correctly shows an array having offsets 0 to 2, with the correct values. But I still get the notice.
Another example is I get an array (from an AJAX call, I json_decode it, etc), then I typed the following code:
foreach (array_keys($myDecodedArray) as $k) {
$value = $myDecodedArray[$k];
someOtherCode();
}
I had that damn notice appear when trying to read $myDecodedArray[$k], although php itself had just told me the key existed !
So, I solved that last case by going
foreach ($myDecodedArray as $k => $value) {
someOtherCode();
}
but still, this is extremely annoying, and makes no sense to me.
Any of you run into that problem before?
Do you have any information about what could cause that?
[EDIT]
Rahul Meshram's suggestion (which I upvoted in the comments) solved my second problem case.
However, the first case still happens (exploding a string into an array, then trying to access that array's values by their numeric keys).
The keys ARE numeric (gettype returns 'integer', var_dump on that key shows an integer too, with the right value), but trying to access $explodedArray[1] still results in that notice being displayed, despite $explodedArray having keys 0, 1, and 2, with associated values.

Warning: array_keys() expects parameter 1 to be array, null given, although array appears properly formed?

I'm working with user data in WordPress, but I think this error is more of a general PHP issue on my end.
I use a function wp_update_user() which takes an array of keys and values corresponding to certain user fields, like display_name.
The code responsible looks as follows (note that I'm hardcoding the value for display_name for debugging purposes):
$returnValue = wp_update_user(array( 'ID' => $user->ID, 'display_name' => 'Test 123' ));
if (is_wp_error($returnValue)) {
print_r($returnValue);
} else {
echo "User update was a success, ID returned is " . $returnValue;
}
The if statement would output an error object if there is an error. However, the value is updating in the database as expected, and no error is returned from WP. The server, however, is giving me a series of warnings, which begin with Warning: array_keys() expects parameter 1 to be array, null given in /srv/www/mysite.com/current/web/wp/wp-includes/user.php on line 1993. I looked up the function array_keys() but as far as I can tell, I'm passing a correctly-formed array as required. This warning does stem from the wp_update_user() function.
As stated above, the actual code seems to work and do what I want it to do, so this warning isn't actually impeding any functionality. I suppose I can hide this warning output but in the interest of best practices I would like to get to the bottom of it. How can I solve this?
Pretty sure its not the parameter you pass that is wrong , but an internal WP process.Try calling that with a plain integer as id of an already existing user

Illegal string offset when printing array values in PHP

Looked at many similiar problems and the solutions didn't help me. I'm getting a strange error message Warning: Illegal string offset 'officeName' in... and the var_dump of the variable generating the error looks like this:
array(10) {
["officeId"]=>
string(5) "11237"
["officeName"]=>
string(37) "Pro Office Inc."
}
The code that produces the errors is:
foreach($objects as $key => $value){
var_dump($value);
}
So $value is an array. What's wrong with what I'm doing and how can I fix it?
Make a double check of every value from the array, since one of these items may be an array, but there may be others which are not. In such cases it is always better to var_dump() the whole $objects array instead of each item, since it's easier to spot any error.
Besides, the Illegal string offset error usually hints that you're trying to treat a string as an array and/or access it's keys, which don't exist.

type casting from array to string for php function argument

I have the following snippet of code.
<?php
$arr = array('str' => 'arr');
var_dump(htmlspecialchars($arr));
?>
And has the following output.
Warning: htmlspecialchars() expects parameter 1 to be string, array given in /Users/shodoco/test.php on line 3
NULL
I am wondering why I am getting this output. According to the PHP manual here, arrays are always converted to the string "Array". htmlspecialchars() takes its first argument as a string, and my understanding is the array should be implicitly converted to string "Array". But in this example, I am getting NULL. What happens here?
Can you try this:
echo "<pre>";
print_r($yourArray);
echo "<pre>";
don't change anything on there, just keep the array as is, i find this the best way of inspecting arrays as strings.
hope that answers your query?

PHP - Function name from variable

Okay, I've found a possible solution for this, but for some reason, I can't make it work in my application. Apparently, if I have a variable which contains a name function, I could use
<?php echo $variable(); ?>
to output the function with the same name.
I'm using Codeigniter. It has a function in its Form helper to output a text field, which is
<?php form_input(); ?>
I have a variable
<?php $instance['taxon_field'] = 'form_input'; ?>
If I echo out this variable, I do get the needed value, 'form_input'. However, as soon as I try to echo
$instance['taxon_field']()
I get a warning:
Message: Illegal string offset 'taxon_field'
and a fatal error:
Call to undefined function p()
I am really clueless here, because echoing only the variable gives 'form_input', but echoing $variable() only gives 'p'.
Where am I doing wrong?
The actual problem here is that $instance is not an array, but a string. Judging from the error message, it's a string whose value starts with p.
The syntax $var[$key] is used not only to access array elements but also to index into strings, where $var[0] would be the first character (actually, byte) of $var etc. If $instance is a string and you write $instance['taxon_field'] then PHP will try to convert 'taxon_field' to an integer in order to index into the string. This results in 0 as per the usual conversion rules, so the whole expression gets you the first letter of the string.
Assuming that the string starts with p it's then pretty obvious why it tries to call a function with that name.
Use call_user_func()
call_user_func($instance['taxon_field']);
The confusion created is actually my own fault because I failed to provide some aditional information which I thought was not important, but turned out to be crutial. My $instance[] array is actually a result of a foreach loop (two of them, to be precise) and is a part of a bigger multidimensional array. The actual code is more complicated, but I'll try to represent it right:
<?php
$bigger_array = array(
0 => array(
'field_one' => 'value_one',
'field_two' => 'value_two',
'field_three' => 'new_function'
),
1 => array(
'field_one' => 'new_value_one',
'field_two' => 'new_value_two',
'field_three' => 'echo'
)
);
function new_function()
{
echo 'New function called.';
}
foreach($bigger_array as $instance)
{
$name = $instance['field_three'];
$name('Hello World!');
}
?>
This will output the following:
New function called.
Fatal error: Call to undefined function echo() in /opt/lampp/htdocs/bla.php on line 69
In other words, the newly defined function works fine, but the built-in 'echo' doesn't.
This is actually not my original problem, this is something that I've encountered while trying to debug the initial issue. And the original problem is that creating a function from a single-dimensional array works okay. whereas creating a function from a multi-dimensional array within a foreach loop transforms the array into a string with the value of its last member.
Now, I'm still not really able to fully answer my question, but I think information I'm giving could lead to a solution. In the simplified example that I gave here, why am I getting the message that echo() function is not defined, while the new function works fine?

Categories