This question already has answers here:
"Notice: Undefined variable", "Notice: Undefined index", "Warning: Undefined array key", and "Notice: Undefined offset" using PHP
(29 answers)
Closed 5 years ago.
I'm continually getting the above error when importing data from csv using the following code:
$csv_data=file_get_contents("data3_received.csv");
foreach(preg_split("/((\r?\n)|(\r\n?))/", $csv_data) as $line){
list($service_id, $ki3) = explode(',', $line,2);
The data imports as expected but the error log is filling up as we utilise the same code in multiple php scripts. The error is on the following line:
list($service_id, $ki3) = explode(',', $line,2);
Tried using the suggestion here: Undefined offset error on php when importing a CSV to no avail, and other on the site.
Any help with this would be most welcome.
Since list() is attempting to assign two variables it is attempting to access two array elements [0] and [1]. Because there is no comma on the line, [1] does not exist.
Try some different functions:
$csv_data = file("data3_received.csv", FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
foreach($csv_data as $line) {
$data[] = str_getcsv($line);
}
To get individual variables you need to check if you have more than one column, or something similar:
if(count($data = str_getcsv($line)) > 1) {
list($service_id, $ki3) = $data;
} else {
$service_id = $data[0];
}
Related
This question already has answers here:
"Notice: Undefined variable", "Notice: Undefined index", "Warning: Undefined array key", and "Notice: Undefined offset" using PHP
(29 answers)
Closed 2 years ago.
any help you offer on this will be useful. Am working on my school project and this error code keeps popping up on the application "Undefined index: client_id" on line 1001"
Here is the code
public function onNewclientaddress(){
$addShipmentForm = Settings::get('addShipmentForm',true);
$data = post();
= \Spot\Shipment\Models\Address::where('user_id', $data['client_id'])->update(['default' => 0]);
if ( $addShipmentForm == "add_form_normal"){
$subitem = new \Spot\Shipment\Models\Address;
$subitem->name = htmlspecialchars($data['street_addr']);
$subitem->user_id = htmlspecialchars($data['client_id']);
$subitem->street = htmlspecialchars($data['street_addr']);
$subitem->city = htmlspecialchars($data['city_id']);
$subitem->zipcode = htmlspecialchars($data['postal_code']);
$subitem->country = htmlspecialchars($data['country_id']);
$subitem->default = 1;
$subitem->created_at = \Carbon\Carbon::now();
$subitem->updated_at = \Carbon\Carbon::now();
$subitem->save();
}
else{
line 1001 is the first line of code in the post. please pardon my English
This error indicates that $data['client_id'] is not being set. You will need to ensure that whatever form is providing this data, is passing client_id correctly.
You can see what is currently in $data with a line like:
die(var_dump($data));
This will output the data on the page.
Ensure your client_id field in your form has a name attribute:
name="client_id"
This question already has answers here:
"Notice: Undefined variable", "Notice: Undefined index", "Warning: Undefined array key", and "Notice: Undefined offset" using PHP
(29 answers)
Closed 4 years ago.
I'm getting an undefined offset error
<?php
function ara($one, $two, $three)
{
#preg_match_all('/' . preg_quote($one, '/') .
'(.*?)'. preg_quote($two, '/').'/i', $three, $m);
return #$m[1];
}
$link = "example.com";
$article = file_get_contents($link);
$sport = ara('data-bin="','"',$article);
$channel = ara('data-videobin="','"',$article);
for ($i=0;$i<50;$i++)
echo"<span class='linko'><a target='_blank' href='example.org/live1.php?id=".($channel[$i]."'>$sport[$i]</a>"."<br></span>"); // error is here
?>
How can I fix it? I'am waiting for your helps. Thanks.
These codes are for a live channel website.
The channel variable has less than 50 elements.
Usually in a loop, you should set the end condition to the size of the array.
for ($i=0;$i<sizeof($channel);$i++)
I have a script which imports data from csv and creates variables from the data which are imported into sql tables. It all works fine but the error log goes crazy with Undefined Offset errors.
From what I have read it is because it only finds index [0] so any others throw up the error.
I've looked at the following and can't seem to get the suggestions to work:
PHP: "Notice: Undefined variable", "Notice: Undefined index", and "Notice: Undefined offset"
PHP Notice: Undefined offset error when importing from .csv
and many more...
Here is the code:
$filename='XXXXX_'.$today.'.csv';
$url=$proto.$user.':'.$pass.'#'.$server.$path.$filename;
$csv_data=file_get_contents($url);
foreach(preg_split("/((\r?\n)|(\r\n?))/", $csv_data) as $line){
list($type, $id, $ref) = explode(',', $line);
The error is generated on the 'list' and 'explode' line of code.
Apologies, quite new to this, have read multiple articles on the 'why' just not found a fix, it is the only error type i have populating the errorlog. Any help would be much appreciated.
you explode must return least 3 output , if in the line exist less than 3 item , error will be show. For this case you can use:
$lineData = explode(','$line);
$type = isset($lineData[0]) ? $lineData[0] : null;
$id = isset($lineData[1]) ? $lineData[1] : null;
$ref = isset($lineData[2]) ? $lineData[2] : null;
Or use:
#list($type, $id, $ref) = explode(',', $line);
This question already has answers here:
"Notice: Undefined variable", "Notice: Undefined index", "Warning: Undefined array key", and "Notice: Undefined offset" using PHP
(29 answers)
Closed 7 years ago.
I have made an sms handler system, and everything is okey, working fine.
But I get this error: PHP Notice: Undefined offset: 1
Its in the 17. line $user is the 17. line, I know its just notice, but daily 20-30 "notice" is in my php error log, and I wanted to fix this.
I tryed many different method, but no changes.
Somebody can help to fix it? Thanks!
$conn = sqlsrv_connect($serverName, $connectionInfo);
$id = $_GET["id"];
$from = $_GET["from"];
$to = $_GET["to"];
$msg = explode(" ", $_GET['message']);
$user = substr(trim($msg[1]),0,10);
Viewing this code helps less to understand but still i would recommend you to place
if(isset($msg[1]) && $msg[1] != ''){
$user = substr(trim($msg[1]),0,10);
} else {
$user = '';
}
because it looks like in some cases $msg[1] does not exist. For example if $_GET['message'] = 'Hello';
Well, $_GET['message'] does not seem to contain a second element. Are you sure its set?
Your code should handle this nicely by having an if or sthg similar. Examples:
$user = "anonymous";
if (sizeof($msg)) > 1) {
$user = substr(trim($msg[1]),0,10);
}
This question already has answers here:
"Notice: Undefined variable", "Notice: Undefined index", "Warning: Undefined array key", and "Notice: Undefined offset" using PHP
(29 answers)
Closed 8 years ago.
when the below code works I get the above mentioned error , what should i do ? please help me ...
<?php
//session_start();
include("dbconnect_database.php");
$tname=$_GET['tn'];
$cname=$_GET['cn'];
$des=mysql_query("desc `$tname` `$cname`");
$row=mysql_fetch_array($des);
list($type, $b) = explode('[(]',$row[1]);
list($size) = explode('[)]',$b);
?>
Try changing this:
list($type, $b) = explode('[(]',$row[1]);
to this:
list($type, $b) = explode('[(]',$row[0]);
UPDATE
The error is telling you that 1 is not a valid index for $row, so that is the problem. Just before that line, try var_dump($row). This will tell what the valid indexes for $row are, and you should be able to use that to fix your code.
there is no ' around table and column name
$des=mysql_query("desc $tname $cname");
sidenote: use mysqli_query instead of mysql because use of mysql is been deprecated