undefined offset when using php explode()
this is because your fullname doesn't contain a space. You can use a simple trick to make sure the space is always where
$split = explode(' ', "$fullname ");
(note the space inside the quotes)
BTW, you can use list() function to simplify your code
list($first, $last) = explode(' ', "$fullname ");
PHP: undefined offset in explode()
list($firstname, $lastname) = array_pad(explode(' ', $queryString, 2), 2, null);
The 2
in explode()
ensures, that there are at most 2 values and array_pad()
ensures, that there are at least 2 values. If there is no space character ,
$lastname
is null
. This you can use to decide what comes next
$lastname = is_null($lastname) ? $firstname : $lastname;
Little update: For this specific case you can use a little trick
list($firstname, $lastname) = array_pad(explode(' ', $queryString, 2), 2, $queryString);
This will do all that in one step. It should work, because
- There is always at least one value (for
$firstname
) - If there is one value, then
$queryString == $firstname
. Thats now the value that is used to fill the array up to 2 values (which is exactly one, because one value we already have) - If there are two values, then the array is not filled with
$queryString
, because we already have 2 values
At least for readability I would prefer the first more obvious solution.
Exploding a line from a file gives Undefined Offset error in PHP
A quick fix, would be to check that you have a line with 7 elements, so after your explode, just check how many elements the array has and skip the rest of the processing if there aren't enough values...
$pieces = explode(",", $line);
if ( count($pieces) != 7) {
continue;
}
$date = $pieces[0];
// ....
You should also look into using prepared statements as the offer more security, although you will still have to do a substitution for the table name as you do now (you can't have a table name as a bind variable).
Also as a recommendation, when doing an INSERT,always list the column names ...
INSERT INTO $tableName (date, open, ... )
VALUES ('$date', '$open', ...)
as this ensures that it's clear which column is where and if any table changes are made that the INSERT is explicit about which values it is using for which columns.
Undefined offset: 1 error daterange explode array
you need to change since range is $range='09/11/2017 - 10/10/2017';
.you need to split using -
$date = explode('-', $range);
if you print $date
Array
(
[0] => 09/11/2017
[1] => 10/10/2017
)
Also note you can pass data directly.You will get instead of [$date[0], $date[1]
since whereBetween
accept array and also $date
is an array
$temp = Temps::select('temp')
->orderBy('date_temp', 'asc')
->whereBetween('date_temp',$date)
->get()
->pluck('temp');
How to avoid undefined offset
list($func, $field) = array_pad(explode('|', $value, 2), 2, null);
Two changes:
- It limits the size of the array returned by
explode()
to 2. It seems, that no more than this is wanted - If there are fewer than two values returned, it appends
null
until the array contains 2 values. See Manual: array_pad() for further information
This means, if there is no |
in $value
, $field === null
. Of course you can use every value you like to define as default for $field
(instead of null
). Its also possible to swap the behavior of $func
and $field
list($func, $field) = array_pad(explode('|', $value, 2), -2, null);
Now $func
is null
, when there is no |
in $value
.
How fix the function to avoid error logs (Undefined offset: 2 in ....) - explode
When you explode $str
you get an array indexed from 0
to n
as result. When you try to access an index that is not available an error occurs.
$bits = explode('/',$str);
var_dump($bits);
Try var_dump
and you can see the array indexes after exploding. Check if it has an index 2
.
UPDATE
$num = isset( $bits[2] ) ? $bits[2] : "";
explode() give me Undefined offset in php
There is if condition error with your code Replace <=
with <
.
for($i=0; $i< count($array_gr);$i++){
$corentimg=$array_gr[$i];
............
PHP Notice: Undefined offset: 1 with array when reading data
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.
Related Topics
Php: How to Convert Bigint from Int to String
Codeigniter PHP Stylesheet Link. How
Do I Need a Trailing Semicolon Here
Sort Array by Value Alphabetically PHP
Retrieving Get and Post Data Inside Laravel Controller
How to Merge Subarray Values and Remove Duplicates
Confirmation That PHP Static Variables Do Not Persist Across Requests
Why Is the Semicolon Optional in the Last Statement in PHP
How Does PHP Index Associative Arrays
How to Examine Defined Constants in PHP
Troubleshooting "Unexpected T_Echo" in Ternary Operator Statement
Php: Split String on Comma, But Not When Between Braces or Quotes
Why Does (0 == 'Hello') Return True in PHP
Wrap Dom Element in Another Dom Element in PHP
Query on a Many-To-Many Relationship Using Doctrine with Symfony2
Calculate the Geo Position at Given Time
How to Replace Multiple Values in PHP
Is There a Function Similar to Settimeout() (Javascript) for PHP