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.
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.
I am unserializing below string but its not returning anything.
a:57:{s:10:"THEME_NAME";s:5:"green";s:16:"PUBLIC_ADS_LIMIT";s:1:"5";s:17:"PUBLIC_EDIT_LIMIT";s:1:"5";s:8:"SITENAME";s:10:"Abcdubai";s:5:";}
The data you're unserializing is wrong
a:57{s:10:"THEME_NAME";s:5:"green";s:16:"PUBLIC_ADS_LIMIT";s:1:"5";s:17:"PUBLIC_EDIT_LIMIT";s:1:"5";s:8:"SITENAME";s:10:"Abcdubai";s:5:";}
if you try to unserialize it with error reporting E_ALL you will see a notice
$data = 'a:57{s:10:"THEME_NAME";s:5:"green";s:16:"PUBLIC_ADS_LIMIT";s:1:"5";s:17:"PUBLIC_EDIT_LIMIT";s:1:"5";s:8:"SITENAME";s:10:"Abcdubai";s:5:";}';
var_dump($data);
you will get
Notice: unserialize(): Error
because
a:57 is the array length and from the data you have it's clearly not 57.
s: points to the length of string s:10:"Abcdubai" the string Abcdubai is not 10 in length it's 8 so you need to change that to s:8:"Abcdubai"
Finally you have s:5:"; at the end for the same reason s:5 means a string with 5 characters in length and it's empty with a one double quote
<?php
// this the valid data
$data = 'a:4:{s:10:"THEME_NAME";s:5:"green";s:16:"PUBLIC_ADS_LIMIT";s:1:"5";s:17:"PUBLIC_EDIT_LIMIT";s:1:"5";s:8:"SITENAME";s:8:"Abcdubai";}';
$data = unserialize($data);
// accessing the valid serialized data
echo $data['THEME_NAME'];
echo $data['PUBLIC_ADS_LIMIT'];
echo $data['PUBLIC_EDIT_LIMIT'];
echo $data['SITENAME'];
you can try this method to solve formatting issues
function fixUnserializeFormatting($data){
// fix string length (will fix s:)
$data = preg_replace('!s:(\d+):"(.*?)";!e', "'s:'.strlen('$2').':\"$2\";'", $data);
// remove empty matches with one double qoute
$data = preg_replace('/s\:+[0-9]+\:";/i', '', $data);
// trying to get the right array length
$strings = substr_count($data,';') / 2;
// fixing array length
$data = preg_replace('/^a:+[0-9]+:/i', "a:{$strings}:", $data);
// finally returning the formatted data
return $data;
}
Usage
$data = 'a:57:{s:10:"THEME_NAME";s:5:"green";s:16:"PUBLIC_ADS_LIMIT";s:1:"5";s:17:"PUBLIC_EDIT_LIMIT";s:1:"5";s:8:"SITENAME";s:10:"Abcdubai";s:5:";}';
$data = fixUnserializeFormatting($data);
var_dump(unserialize($data));
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 am using Codeigniter framework of PHP and trying to extract keywords from the page. The complete code for reference can be seen here. It is not ready-made though.
The issue is due to the array function in the following line:
$keywordCounts = array_count_values( $words );
The error message being displayed is as follows:
A PHP Error was encountered
Severity: Warning
Message: array_count_values() [function.array-count-values]: Can only count STRING and INTEGER values!
EDITED: The array $words for reference can be found here.
There are no special symbols or invalid characters to my knowledge in the $words array. Hyphens and periods are not read by the function or is there some other issue ?
you have null values in your array. you have to replace them before working with array_count_values like this:
$x = array('s'=>'ss', 'a',4 , 'sss' => null);
$ar = array_replace($x,array_fill_keys(array_keys($x, null),''));
$v = array_count_values($ar);
var_dump($v);
which will result:
array (size=4)
'ss' => int 1
'a' => int 1
4 => int 1
'' => int 1