I am using the explode function in php to split a string like "60-5000"
into two elements that can be cast to integers.
$parts = explode('-', '60-5000',2);
$this->p1 = (integer)$parts[0];
$this->p2 = (integer)#$parts[1];
When this is ran in the constructor of my class I am getting this error:
Undefined offset: 1
When I suppress the error on the second element of the array I obtain the correct array
0 => "60"
1 => "5000"
Above is the debug statement with error suppressed on the second index.
I do not want to keep having to suppress this error to get the results from explode. Is there something wrong with my implementation?
$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.
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.
I am getting this PHP error:
PHP Notice: Undefined offset: 1
Here is the PHP code that throws it:
$file_handle = fopen($path."/Summary/data.txt","r"); //open text file
$data = array(); // create new array map
while (!feof($file_handle) ) {
$line_of_text = fgets($file_handle); // read in each line
$parts = array_map('trim', explode(':', $line_of_text, 2));
// separates line_of_text by ':' trim strings for extra space
$data[$parts[0]] = $parts[1];
// map the resulting parts into array
//$results('NAME_BEFORE_:') = VALUE_AFTER_:
}
What does this error mean? What causes this error?
Change
$data[$parts[0]] = $parts[1];
to
if ( ! isset($parts[1])) {
$parts[1] = null;
}
$data[$parts[0]] = $parts[1];
or simply:
$data[$parts[0]] = isset($parts[1]) ? $parts[1] : null;
Not every line of your file has a colon in it and therefore explode on it returns an array of size 1.
According to php.net possible return values from explode:
Returns an array of strings created by splitting the string parameter on boundaries formed by the delimiter.
If delimiter is an empty string (""), explode() will return FALSE. If delimiter contains a value that is not contained in string and a negative limit is used, then an empty array will be returned, otherwise an array containing string will be returned.
How to reproduce the above error in PHP:
php> $yarr = array(3 => 'c', 4 => 'd');
php> echo $yarr[4];
d
php> echo $yarr[1];
PHP Notice: Undefined offset: 1 in
/usr/local/lib/python2.7/dist-packages/phpsh/phpsh.php(578) :
eval()'d code on line 1
What does that error message mean?
It means the php compiler looked for the key 1 and ran the hash against it and didn't find any value associated with it then said Undefined offset: 1
How do I make that error go away?
Ask the array if the key exists before returning its value like this:
php> echo array_key_exists(1, $yarr);
php> echo array_key_exists(4, $yarr);
1
If the array does not contain your key, don't ask for its value. Although this solution makes double-work for your program to "check if it's there" and then "go get it".
Alternative solution that's faster:
If getting a missing key is an exceptional circumstance caused by an error, it's faster to just get the value (as in echo $yarr[1];), and catch that offset error and handle it like this: https://stackoverflow.com/a/5373824/445131
Update in 2020 in Php7:
there is a better way to do this using the Null coalescing operator by just doing the following:
$data[$parts[0]] = $parts[1] ?? null;
This is a "PHP Notice", so you could in theory ignore it. Change php.ini:
error_reporting = E_ALL & ~E_NOTICE & ~E_STRICT & ~E_DEPRECATED
To
error_reporting = E_ALL & ~E_NOTICE
This show all errors, except for notices.
my quickest solution was to minus 1 to the length of the array as
$len = count($data);
for($i=1; $i<=$len-1;$i++){
echo $data[$i];
}
my offset was always the last value if the count was 140 then it will say offset 140 but after using the minus 1 everything was fine
The ideal solution would be as below. You won't miss the values from 0 to n.
$len=count($data);
for($i=0;$i<$len;$i++)
echo $data[$i]. "<br>";
In your code:
$parts = array_map('trim', explode(':', $line_of_text, 2));
You have ":" as separator. If you use another separator in file, then you will get an "Undefined offset: 1" but not "Undefined offset: 0" All information will be in $parts[0] but no information in $parts[1] or [2] etc. Try to echo $part[0]; echo $part[1]; you will see the information.
I just recently had this issue and I didn't even believe it was my mistype:
Array("Semester has been set as active!", true)
Array("Failed to set semester as active!". false)
And actually it was! I just accidentally typed "." rather than ","...
The output of the error, is because you call an index of the Array that does not exist, for example
$arr = Array(1,2,3);
echo $arr[3];
// Error PHP Notice: Undefined offset: 1 pointer 3 does not exist, the array only has 3 elements but starts at 0 to 2, not 3!