how to access array in array when inserting json data to database? - php

I have json file that contains of 50 lines of similar
"results":[{"score":0,"team_id":126266},{"score":0,"team_id":125798}].
I need to insert this to database, but I keep getting errors:
Notice: Undefined index: results[0]:score[0] in D:\xampp\htdocs\json\index.php on line 26
Notice: Undefined index: results[1]:score[0] in D:\xampp\htdocs\json\index.php on line 27
Notice: Undefined index: results[0]:team_id[0] in D:\xampp\htdocs\json\index.php on line 32
Notice: Undefined index: results[1]:team_id[0] in D:\xampp\htdocs\json\index.php on line 33
Here's the code:
$pirmo_oponento_rezultatas = $row['results[0]:score[0]'];
$antro_oponento_rezultatas = $row['results[1]:score[0]'];
$fk_Komandosid_Komandos = $row['results[0]:team_id[0]'];
$fk_Komandosid_Komandos1 = $row['results[1]:team_id[0]'];
$sql="INSERT INTO rungtynes (pirmo_oponento_rezultatas,antro_oponento_rezultatas,fk_Komandosid_Komandos,
fk_Komandosid_Komandos1) VALUES ('$pirmo_oponento_rezultatas','$antro_oponento_rezultatas',
'$fk_Komandosid_Komandos','$fk_Komandosid_Komandos1')";

You can access as below
$pirmo_oponento_rezultatas = $row['results'][0]['score'];
$antro_oponento_rezultatas = $row['results'][1]['score'];

Related

error Undefined offset while using explode

$a = explode(",",$data['ids'][$i]);
$dbtvalues = array();
for($j=0;$j< count($a);$j++)
{
$dbtvalues['serial_no'] =$a[$j];
if($data['modelno'] !="select")
{
$dbtvalues['model_no'] = $data['modelno'][$j];
}
$dbtvalues['indent_detail_id1'] = (int)$last_id ;
$data['indent_req_detail_id'][$i];
$this->outpatient_model->insert_row('tra_indent_issue_detail',$dbtvalues);
}
$a need to explode and insert into db if array has 5 values 5 rows must be inserted into table.
getting error message as :
Severity: Notice
Message: Undefined offset: 2
Filename: controllers/indent_receipt.php
Line Number: 71
A PHP Error was encountered
Severity: Notice
Message: Undefined offset: 3
Filename: controllers/indent_receipt.php
Line Number: 71
A PHP Error was encountered
Severity: Notice
Message: Undefined offset: 4
Filename: controllers/indent_receipt.php
Line Number: 71
A PHP Error was encountered
Severity: Notice
Message: Undefined offset: 5
Filename: controllers/indent_receipt.php
Line Number: 71
A PHP Error was encountered
Severity: Notice
Message: Undefined offset: 2
Filename: controllers/indent_receipt.php
Line Number: 71
thanks in advance.
Index is not exists. that why warning occurs
$a=array();
if(!empty($data['ids'][$i]))
$a = explode(",",$data['ids'][$i]);
Put above if condition it's working when index is not empty.

Foreach json getting a lot of errors: Trying to get property of non-object / Undefined property

I'm trying to fetch the name and artists of all the tracks on a spotify playlist.
I have no problem fetching the name of the playlist, the coverart, the total count of followers.
But when I try to fetch the name and artist i get this line of errors
Notice: Trying to get property of non-object in /Users/rasmuspedersen/Dropbox/www/life/mobil/json.php on line 14
Notice: Trying to get property of non-object in /Users/rasmuspedersen/Dropbox/www/life/mobil/json.php on line 14
Notice: Trying to get property of non-object in /Users/rasmuspedersen/Dropbox/www/life/mobil/json.php on line 14
Notice: Trying to get property of non-object in /Users/rasmuspedersen/Dropbox/www/life/mobil/json.php on line 14
Notice: Trying to get property of non-object in /Users/rasmuspedersen/Dropbox/www/life/mobil/json.php on line 14
Notice: Undefined property: stdClass::$name in /Users/rasmuspedersen/Dropbox/www/life/mobil/json.php on line 14
Notice: Undefined property: stdClass::$name in /Users/rasmuspedersen/Dropbox/www/life/mobil/json.php on line 14
Notice: Trying to get property of non-object in /Users/rasmuspedersen/Dropbox/www/life/mobil/json.php on line 14
Notice: Trying to get property of non-object in /Users/rasmuspedersen/Dropbox/www/life/mobil/json.php on line 14
Notice: Trying to get property of non-object in /Users/rasmuspedersen/Dropbox/www/life/mobil/json.php on line 14
Notice: Trying to get property of non-object in /Users/rasmuspedersen/Dropbox/www/life/mobil/json.php on line 14
Notice: Trying to get property of non-object in /Users/rasmuspedersen/Dropbox/www/life/mobil/json.php on line 14
Notice: Trying to get property of non-object in /Users/rasmuspedersen/Dropbox/www/life/mobil/json.php on line 14
Notice: Trying to get property of non-object in /Users/rasmuspedersen/Dropbox/www/life/mobil/json.php on line 14
Notice: Trying to get property of non-object in /Users/rasmuspedersen/Dropbox/www/life/mobil/json.php on line 14
my code is
$string = file_get_contents("playlist.json");
$json=json_decode($string);
echo $json->followers->total.'<br />';
foreach($json->images as $image){
echo '<img src="'.$image->url.'" /><br>';
}
echo $json->name.'<br />';
$i = 0;
foreach($json->tracks->items as $tracks){
foreach($tracks->track as $track) {
echo($track->name);
};
if(++$i > 1) break;
};`
And my JSON is this
http://wecre8.it/playlist.json
I can't find the problem, and I tried google.
Hope someone can find my error and help fetch the artist and titles of the tracks in the playlist
In your example JSON, the track object is under the items array and not under tracks object as what your code is looking for.
$string = file_get_contents ( 'playlist.json' );
$array = json_decode ( $string, TRUE );

Notice: Undefined index: category

Notice: Undefined index: category in /opt/lampp/htdocs/content/cron/check_deposits.php on line 18
$deposits=mysql_query("SELECT * FROM `deposits`");
while ($dp=mysql_fetch_array($deposits)) {
$received=0;
$txid='';
$txs=$wallet->listtransactions('',2000);
$txs=array_reverse($txs);
foreach ($txs as $tx) {
if($tx['category']!='receive') continue;
if ($tx['confirmations']<1) continue;
if ($tx['address']!=$dp['address']) continue;
$received=$tx['amount'];
$txid=$tx['txid'];
break;
This is how it came out of the box. I contacted support about the issue and they told me to make sure I was using PHP 5.3.1 which I am. What is wrong with this?
Here is a link to the full code:
http://diceking.tk/deposit.txt
You need to change from
if($tx['category']!='receive') continue;
to
if(isset($tx['category']) && $tx['category']!='receive') continue;
Check if the variables are set.
So, for the error about confirmations, you can use this:
if(isset($tx['confirmations']) && $tx['confirmations'] < 1) continue;
For the error about the undefined index: Category you can use this:
if(isset($tx['category']) && $tx['category']!='receive') continue;
I hope I'll helped you.
If this helped, please vote my answer.
Notice: Undefined index: amount in /opt/lampp/htdocs/content/cron/check_deposits.php on line 21
Notice: Undefined index: txid in /opt/lampp/htdocs/content/cron/check_deposits.php on line 22
Notice: Undefined index: amount in /opt/lampp/htdocs/content/cron/check_deposits.php on line 21
Notice: Undefined index: txid in /opt/lampp/htdocs/content/cron/check_deposits.php on line 22
Ahh so we have made it this far.. I tried this trick and it hated me for it:
if(isset($received=$tx['amount']) && $received=$tx['amount']) continue;

Referencing a dynamic variable?

for ($count = 1; $count <= 5; ++$count) {
$test = ${'node->field_aw_score_' . $count}[LANGUAGE_NONE][0]['value'];
echo $test;
}
Throws the error:
Notice: Undefined variable: node->field_aw_score_1
Notice: Undefined variable: node->field_aw_score_2
Notice: Undefined variable: node->field_aw_score_3
Notice: Undefined variable: node->field_aw_score_4
Notice: Undefined variable: node->field_aw_score_5
However, the variables do exist. I'm trying to reference:
$node->field_aw_score_1[LANGUAGE_NONE][0]['value']
then
$node->field_aw_score_2[LANGUAGE_NONE][0]['value']
etc. A dynamic variable. What am I doing wrong? Thanks.
Try this instead:
$node->{'field_aw_score_' . $count}

Undefined index while using MySQLi fetch_assoc [duplicate]

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 9 years ago.
I'm receiving the following errors when I run my PHP script. The script is supposed to do some mapping in the database via JOINs, and then export the results to a CSV file.
Notice: Undefined index: partterminologyname in /home/icarcare/public_html/magmi/integration/scripts/fileimport.php on line 358
Notice: Undefined index: YearID in /home/icarcare/public_html/magmi/integration/scripts/fileimport.php on line 359
Notice: Undefined index: MakeName in /home/icarcare/public_html/magmi/integration/scripts/fileimport.php on line 360
Notice: Undefined index: modelname in /home/icarcare/public_html/magmi/integration/scripts/fileimport.php on line 361
Notice: Undefined index: SubModelName in /home/icarcare/public_html/magmi/integration/scripts/fileimport.php on line 362
Notice: Undefined index: EngineDesignationName in /home/icarcare/public_html/magmi/integration/scripts/fileimport.php on line 363
Notice: Undefined index: EngineVINName in /home/icarcare/public_html/magmi/integration/scripts/fileimport.php on line 364
Notice: Undefined index: Liter in /home/icarcare/public_html/magmi/integration/scripts/fileimport.php on line 365
Notice: Undefined index: CC in /home/icarcare/public_html/magmi/integration/scripts/fileimport.php on line 366
Notice: Undefined index: CID in /home/icarcare/public_html/magmi/integration/scripts/fileimport.php on line 367
Notice: Undefined index: Cylinders in /home/icarcare/public_html/magmi/integration/scripts/fileimport.php on line 368
Notice: Undefined index: BlockType in /home/icarcare/public_html/magmi/integration/scripts/fileimport.php on line 369
Notice: Undefined index: EngBoreIn in /home/icarcare/public_html/magmi/integration/scripts/fileimport.php on line 370
Notice: Undefined index: EngBoreInMetric in /home/icarcare/public_html/magmi/integration/scripts/fileimport.php on line 371
Notice: Undefined index: EngStrokeIn in /home/icarcare/public_html/magmi/integration/scripts/fileimport.php on line 372
Notice: Undefined index: EngStrokeMetric in /home/icarcare/public_html/magmi/integration/scripts/fileimport.php on line 373
Notice: Undefined index: FuelDeliveryTypeName in /home/icarcare/public_html/magmi/integration/scripts/fileimport.php on line 374
Notice: Undefined index: FuelDeliverySubTypeName in /home/icarcare/public_html/magmi/integration/scripts/fileimport.php on line 375
Notice: Undefined index: FuelSystemControlTypeName in /home/icarcare/public_html/magmi/integration/scripts/fileimport.php on line 376
Notice: Undefined index: FuelSystemDesignName in /home/icarcare/public_html/magmi/integration/scripts/fileimport.php on line 377
Notice: Undefined index: AspirationName in /home/icarcare/public_html/magmi/integration/scripts/fileimport.php on line 378
Notice: Undefined index: CylinderHeadTypeName in /home/icarcare/public_html/magmi/integration/scripts/fileimport.php on line 379
Notice: Undefined index: FuelTypeName in /home/icarcare/public_html/magmi/integration/scripts/fileimport.php on line 380
Notice: Undefined index: IgnitionSystemTypeName in /home/icarcare/public_html/magmi/integration/scripts/fileimport.php on line 381
Notice: Undefined index: EngineMfr.MfrName in /home/icarcare/public_html/magmi/integration/scripts/fileimport.php on line 382
Notice: Undefined index: EngineVersion in /home/icarcare/public_html/magmi/integration/scripts/fileimport.php on line 383
Notice: Undefined index: ValvesPerEngine in /home/icarcare/public_html/magmi/integration/scripts/fileimport.php on line 384
Notice: Undefined index: BedLength in /home/icarcare/public_html/magmi/integration/scripts/fileimport.php on line 385
Notice: Undefined index: BedLengthMetric in /home/icarcare/public_html/magmi/integration/scripts/fileimport.php on line 386
Notice: Undefined index: BedTypeName in /home/icarcare/public_html/magmi/integration/scripts/fileimport.php on line 387
Notice: Undefined index: BodyNumDoors in /home/icarcare/public_html/magmi/integration/scripts/fileimport.php on line 388
Notice: Undefined index: BrakeSystemName in /home/icarcare/public_html/magmi/integration/scripts/fileimport.php on line 389
Notice: Undefined index: BrakeABSName in /home/icarcare/public_html/magmi/integration/scripts/fileimport.php on line 390
Notice: Undefined index: BrakeTypeName in /home/icarcare/public_html/magmi/integration/scripts/fileimport.php on line 391
Notice: Undefined index: BrakeFront.BrakeTypeName in /home/icarcare/public_html/magmi/integration/scripts/fileimport.php on line 392
Notice: Undefined index: BrakeRear.BrakeTypeName in /home/icarcare/public_html/magmi/integration/scripts/fileimport.php on line 393
Notice: Undefined index: DriveTypeName in /home/icarcare/public_html/magmi/integration/scripts/fileimport.php on line 394
Notice: Undefined index: MfrBodyCodeName in /home/icarcare/public_html/magmi/integration/scripts/fileimport.php on line 395
Notice: Undefined index: SpringTypeFront.SpringTypeName in /home/icarcare/public_html/magmi/integration/scripts/fileimport.php on line 396
Notice: Undefined index: SpringTypeRear.SpringTypeName in /home/icarcare/public_html/magmi/integration/scripts/fileimport.php on line 397
Notice: Undefined index: SteeringTypeName in /home/icarcare/public_html/magmi/integration/scripts/fileimport.php on line 398
Notice: Undefined index: TransmissionTypeName in /home/icarcare/public_html/magmi/integration/scripts/fileimport.php on line 399
Notice: Undefined index: TransmissionNumSpeeds in /home/icarcare/public_html/magmi/integration/scripts/fileimport.php on line 400
Notice: Undefined index: TransmissionControlTypeName in /home/icarcare/public_html/magmi/integration/scripts/fileimport.php on line 401
Notice: Undefined index: TransmissionMfrCode in /home/icarcare/public_html/magmi/integration/scripts/fileimport.php on line 402
Notice: Undefined index: TransmissionMfr.MfrName in /home/icarcare/public_html/magmi/integration/scripts/fileimport.php on line 403
Notice: Undefined index: ElecControlled in /home/icarcare/public_html/magmi/integration/scripts/fileimport.php on line 404
Notice: Undefined index: WheelBase in /home/icarcare/public_html/magmi/integration/scripts/fileimport.php on line 405
Notice: Undefined index: WheelBaseMetric in /home/icarcare/public_html/magmi/integration/scripts/fileimport.php on line 406
Notice: Undefined index: VehicleTypeName in /home/icarcare/public_html/magmi/integration/scripts/fileimport.php on line 407
The code related to this is as follows...
// Write all the user records to the spreadsheet
/* fetch associative array */
while ($row = $results->fetch_assoc()) {
fputcsv($handle, array(
'admin',
'base',
'Default',
'simple',
'2',
$brandprefix . $row['part_id'],
$row['part_id'],
'1',
'1',
'Enabled',
'Description',
'Short Description',
$row['partterminologyname'],
$row['YearID'],
$row['MakeName'],
$row['modelname'],
$row['SubModelName'],
$row['EngineDesignationName'],
$row['EngineVINName'],
$row['Liter'],
$row['CC'],
$row['CID'],
$row['Cylinders'],
$row['BlockType'],
$row['EngBoreIn'],
$row['EngBoreInMetric'],
$row['EngStrokeIn'],
$row['EngStrokeMetric'],
$row['FuelDeliveryTypeName'],
$row['FuelDeliverySubTypeName'],
$row['FuelSystemControlTypeName'],
$row['FuelSystemDesignName'],
$row['AspirationName'],
$row['CylinderHeadTypeName'],
$row['FuelTypeName'],
$row['IgnitionSystemTypeName'],
$row['EngineMfr.MfrName'],
$row['EngineVersion'],
$row['ValvesPerEngine'],
$row['BedLength'],
$row['BedLengthMetric'],
$row['BedTypeName'],
$row['BodyNumDoors'],
$row['BrakeSystemName'],
$row['BrakeABSName'],
$row['BrakeTypeName'],
$row['BrakeFront.BrakeTypeName'],
$row['BrakeRear.BrakeTypeName'],
$row['DriveTypeName'],
$row['MfrBodyCodeName'],
$row['SpringTypeFront.SpringTypeName'],
$row['SpringTypeRear.SpringTypeName'],
$row['SteeringTypeName'],
$row['TransmissionTypeName'],
$row['TransmissionNumSpeeds'],
$row['TransmissionControlTypeName'],
$row['TransmissionMfrCode'],
$row['TransmissionMfr.MfrName'],
$row['ElecControlled'],
$row['WheelBase'],
$row['WheelBaseMetric'],
$row['VehicleTypeName']
));
}
I tried to echo one of the row values and then issue a die();, and the value echoed properly. I'm wondering if I'm missing something here... thanks!
Before you fetch the array, you must be validate if the record has some value:
if (count($row) > 1) {
//Fetch the items
}
The most common reason for this error, is because you are trying to use a data that doesn't exists.
Don't forget initialize your variables too.
$row = array()
Greetings

Categories