Hi I am getting the following message in my wordpress filter page for a holidays website:
Warning: Illegal string offset 'order' in /home/jefmaher/public_html/wp-content/themes/thevacationrental/functions.php on line 916
Warning: Illegal string offset 'order' in /home/jefmaher/public_html/wp-content/themes/thevacationrental/functions.php on line 917
This is the snippet of php
$i = 9999;
foreach( $options['customdatas'] as $key => $data ) {
if( empty( $data['order'] ) ) {
$options['customdatas'][$key]['order'] = $i;
$data['order'] = $i;
}
I am new to php and need some advice please
thanks
Jeff
"Illegal string offset" message indicates $data is not an array but rather a string, in, at least, one of the cases of foreach. One way to fix is to check if $data is, in fact an array.
if (is_array($data)) ;//
Related
PHP Warning: Illegal string offset 'user_login' in /home/.../wp-includes/user.php on line 83
PHP Warning: Illegal string offset 'user_password' in /home/.../wp-includes/user.php on line 83
Here the code:-
add_filter('authenticate', 'wp_authenticate_cookie', 30, 3);
$user = wp_authenticate($credentials['user_login'], $credentials['user_password']);
if ( is_wp_error($user) ) {
if ( $user->get_error_codes() == array('empty_username', 'empty_password') ) {
$user = new WP_Error('', '');
}
return $user;
}
Obviously $credentials is not that array. Show var_dump($credentials);
The error Illegal string offset 'whatever' in... generally means: you're trying to use a string as a full array.
That is actually possible since strings are able to be treated as arrays of single characters in PHP. So you're thinking the $var is an array with a key, but it's just a string with standard numeric keys
Try adding following lines before $ user
if(isset($_POST)){
$credentials = array(
'user_login' => isset($_POST['log']) ? $_POST['log'] : '',
'user_password' => isset($_POST['pwd']) ? $_POST['pwd'] :'',
);
}
$X['high'] = 1234;
$var = array("X","high");
This is working:
$temp = $$var[0];
$temp = $temp[$var[1]];
echo $temp;
But this isn't working:
echo $$var[0][$var[1]];
Why? How can i make it works?
You should explain to php parser how you want this statement to be parsed:
echo ${$var[0]}[$var[1]];
Without brackets you will have:
php7
Notice: Array to string conversion in /in/cvZqc on line 5
Notice: Undefined variable: Array in /in/cvZqc on line 5
php5
Warning: Illegal string offset 'high' in /in/cvZqc on line 5
Notice: Array to string conversion in /in/cvZqc on line 5
Sample link.
What will be the if statement to echo the strings of the array containing 'A'?
Code:
<?php
$students=array(
array('roll_no'=>1,'name'=>'Sagar','percentage'=>78,'grade'=>'A'),
array('roll_no'=>2,'name'=>'Rahul','percentage'=>50,'grade'=>'C'),
array('roll_no'=>3,'name'=>'Emir','percentage'=>60,'grade'=>'B'),
);
foreach($students as $array)
{
foreach($array as $value)
{
if($value['grade']='A')
{
echo $value;
}
}
}
?>
Output:
Warning: Cannot use a scalar value as an array in C:\xampp\htdocs\php\Assignment41C.php on line 11
Warning: Illegal string offset 'grade' in C:\xampp\htdocs\php\Assignment41C.php on line 11
Aagar
Warning: Cannot use a scalar value as an array in C:\xampp\htdocs\php\Assignment41C.php on line 11
Warning: Illegal string offset 'grade' in C:\xampp\htdocs\php\Assignment41C.php on line 11
A
Warning: Cannot use a scalar value as an array in C:\xampp\htdocs\php\Assignment41C.php on line 11
Warning: Illegal string offset 'grade' in C:\xampp\htdocs\php\Assignment41C.php on line 11
Aahul
Warning: Cannot use a scalar value as an array in C:\xampp\htdocs\php\Assignment41C.php on line 11
Warning: Illegal string offset 'grade' in C:\xampp\htdocs\php\Assignment41C.php on line 11
A
Warning: Cannot use a scalar value as an array in C:\xampp\htdocs\php\Assignment41C.php on line 11
Warning: Illegal string offset 'grade' in C:\xampp\htdocs\php\Assignment41C.php on line 11
Amir
Warning: Cannot use a scalar value as an array in C:\xampp\htdocs\php\Assignment41C.php on line 11
Warning: Illegal string offset 'grade' in C:\xampp\htdocs\php\Assignment41C.php on line 11
A
You don't need second foreach. Every $array value stores all key => value pairs. So you just need to check grade key's value:
foreach ($students as $array) {
// note a double `==` which is a comparison operator
if ($array['grade'] == 'A') {
// print_r instead of `echo` cause `echo` won't output array properly
print_r($array);
}
}
Looks like you have used an extra loop here. Also, you might need to recheck this $value['grade']='A' part. Within if condition, a single = implies assignment operation. Use == or ===.
Try this to print the grade only:
foreach ($students as $array) {
if($array['grade']== 'A') {
echo $array['grade']; // To show the grade only
}
}
Try this to print all fields:
foreach($students as $array){
if($array['grade']== 'A'){
foreach($array as $k => $v) {
echo "$k : $v";
echo "<br/>";
}
}
}
Hope this helps.
Peace! xD
We're running PHP 5.3.10-1ubuntu3.15 with Suhosin-Patch, and I just ran across the weirdest thing. I keep getting an Array to string conversion error.
Here is some code with line numbers:
115 $report['report'][$key]['report'] = array();
116 watchdog('ranking_report_field', 'key is a: ' . gettype($key), array(), WATCHDOG_NOTICE);
117 $report['report'][$key]['report'] = array(
'#markup' => "<p>No information available.</p><p>For questions, <a href='mailto:$emailAddr'>email</a> your account executive ($emailAddr).</p>",
);
Here are Drupal's (sequential) logs for those line numbers:
Notice: Array to string conversion in foo() (line 115 of /var/www/...
key is a: string
Notice: Array to string conversion in foo() (line 117 of /var/www/...
So far as I can tell there's no array to string conversion that should be taking place. Someone help me out with a second pair of eyes, please - or is this some kind of bug that just hit PHP?
One of the array keys is mapped to a string not an array. Here is a program for how such an error could occur.
<?php
$key = 0;
$report = array();
$report['report'] = array();
$report['report'][$key] = 'report';
// Array to string conversion error
$report['report'][$key]['report'] = array();
// what I assume you are expecting is
$report['report'][$key] = array();
$report['report'][$key]['report'] = array(); // no more notices
NOTE: at his time the OP has not included info for how the array is created
I have a php code. I am just trying out to define and get array. The below is the code.
<?php
$query = 'summer';
$query['jink'] = array( 1,4,5,6 );
var_dump($query);
var_dump($query['jink']);
?>
var_dump returns:
string 'Aummer' (length=6)
string 'A' (length=6)
The output is not as expected. it should give something from (1,4,5,6)
I fixed your errors in order to show the issue:
$query = 'summer';
$query['jink'] = array( 1,4,5,6 );
$query is a string "summer" so ['jink'], not being a valid string offset is converted to 0 and it accesses the first character of "summer". Also, array( 1,4,5,6 ) is not a string it is Array and the "A" from Array is assigned to offset 0 of "summer" yielding "Aummer":
var_dump($query);
Now again you are getting string offset 0 which is "A" from "Aummer":
var_dump($query['jink']);
If you use error reporting:
error_reporting(E_ALL);
ini_set('display_errors', '1');
You will see:
PHP Warning: Illegal string offset 'jink' in file line
PHP Notice: Array to string conversion in file line
PHP Warning: Illegal string offset 'jink' in file line
What you maybe want is:
$query = ['summer'];
$query['jink'] = [1,4,5,6 ];
var_dump($query);
var_dump($query['jink']);
PHP Sandbox: http://sandbox.onlinephpfunctions.com/code/50f22fb5de571baf5978ab37e8cd645ec174125e.
Well the error is that you set the $query as a string then turn it into an array.You should simply edit $query = 'summer' to $query[] = 'summer' as this link shows.