Undefined offset:1 - php

$aa = Input::get('AccountOpeningDate' . $i);
$dateinfo = explode("-", $aa);
$testDay = Carbon::createFromDate($dateinfo[0], $dateinfo[1],
$dateinfo[2], 'UTC');
$actualDate = $testDay->setTimezone('+6:00');
when I run this code then I get an output.But it cause an error that like the image below.
ErrorException in MemberController.php line 532:
Undefined offset: 1
in MemberController.php line 532
at HandleExceptions->handleError('8', 'Undefined offset: 1', 'C:\xampp\htdocs\timf\app\Http\Controllers\MemberController.php', '532', array('id' => '4001-5088-0565', 'memberdata' => object(Member), 'somityDay' => object(Zone1), 'i' => '2', 'aa' => '', 'dateinfo' => array(''), 'testDay' => object(Carbon), 'actualDate' => object(Carbon), 'producttype' => '2', 'memberaccount' => object(Accountstable), 'valsa' => object(Product), 'AccNameSub' => 'MSSM', 'accnumber' => 'MSSM.4001-5088-0565', 'k' => '13', 'SavingSetup' =>
This code is written in laravel 5.1.

$aa = Input::get('AccountOpeningDate' . $i);
Here $aa has no data in case of any conditions. So the array $dateinfo remaining empty. I have fixed the problem by ensuring $aa data not empty.
now the code is running well.

There may be a comma missing in your first line of code.

Related

PHP $var += array('x','y')

Sorry to ask, it's only for my understanding !
I just start learning php.
I added some values from different method to an array and it come to a strange problem that i can't find an answer on the web.
(sorry if it's stupid, i just want to know why it do that.)
My PHP/HTML code:
<?php
$test[] = 1;
$test += array('2','3','4');
$test += array('4in4',5 => '5');
$test[] = 6;
$test[] += 7;
?>
<!doctype html>
<html lang="fr-CA" >
<head>
<meta charset="UTF-8">
<body>
<?php echo '<h1>Test de Tableau</h1>','<br>',
'$test[0] = ',$test[0],'<br>',
'$test[1] = ',$test[1],'<br>',
'$test[2] = ',$test[2],'<br>',
'$test[3] = ',$test[3],'<br>',
'$test[4] = ',$test[4],'<br>',
'$test[5] = ',$test[5],'<br>',
'$test[6] = ',$test[6],'<br>',
'$test[7] = ',$test[7],'<br>',
'<h4>count = ',count($test),'/8</h4>'; ?>
</body>
And here is the result :
Test de Tableau
$test[0] = 1
$test[1] = 3
$test[2] = 4
$test[3] =
Notice: Undefined offset: 3 in /opt/lampp/htdocs/mhx/test/index.php on line 26
$test[4] =
Notice: Undefined offset: 4 in /opt/lampp/htdocs/mhx/test/index.php on line 27
$test[5] = 5
$test[6] = 6
$test[7] = 7
count = 6/8
Thanks to answer !
MHX
This may have already been answered by this post: + operator for array in PHP?
Essentially, here's what's happening. You initialized your array:
$test = [0 => 1];
Next, you're adding a new array to it:
[0 => '2', 1 => '3', 2 => '4'];
The first index already exists, so it's skipped giving us:
$test = [0 => 1, 1 => '3', 2 => '4'];
Now, you're adding another array:
[0 => '4in4', 5 => '5'];
Again, the first index exists, so we get:
$test = [0 => 1, 1 => '3', 2 => '4', 5 => '5'];
By now, you can see that offsets 3 and 4 are missing, hence your notices above. Also, the internal pointer is now at 6 since the last element added was at 5.
You then add 6, followed by 7, giving us the final array:
$test = [0 => 1, 1 => '3', 2 => '4', 5 => '5', 6 => 6, 7 => 7];
I hope this helps.
EDIT: When adding another element to an array, you can just write it like this:
$test[] = 1;
If you need to merge two arrays, look at array_merge():
$test = array_merge($test, [1, 2, 3]);
Cheers!

My PHP code serializes, but doesn't unserialize

THis this my code .
$data = array(
'24 Jan|8:30' => '12.6',
'22 Feb|8:30' => '250',
'11 Mar|8:10' => '0',
'31 Apr|23:30' => '7',
'32 Apr|23:30' => '80',
'33 Apr|23:30' => '67',
'34 r|23:30' => '45',
'35 Ap|23:30' => '66',
'34 Lr|23:30' => '23',
'3 Apr|23:30' => '23'
);
//echo serialize($data);
$x = unserialize('a:10:{s:12:"24 Jan|8:30 ";s:4:"12.6";s:12:"22 Feb|8:30 ";s:3:"250";s:12:"11 Mar|8:10 ";s:1:"0";s:12:"31 Apr|23:30";s:1:"7";s:12:"32 Apr|23:30";s:2:"80";s:12:"33 Apr|23:30";s:2:"67";s:12:"34 r|23:30 ";s:2:"45";s:12:"35 Ap|23:30 ";s:2:"66";s:12:"34 Lr|23:30 ";s:2:"23";s:12:"3 Apr|23:30 ";s:2:"23";}');
var_dump($x);
Not work in unserialize function.
Please help!
The serialized representation of $data and the string you are trying to unserialize differ.
http://codepad.viper-7.com/3zlk1a
At offset 199 you see
s:12:"34 r|23:30 "
but the string (s) isn't 12 characters long (thats what s:12: mean). I guess something modified the serialized string directly. Just don't do it :) Always unserialize and work with the structured values.
'a:10:{s:12:"24 Jan|8:30 ";s:4:"12.6";s:12:"22 Feb|8:30 ";s:3:"250";s:12:"11 Mar|8:10 ";s:1:"0";s:12:"31 Apr|23:30";s:1:"7";s:12:"32 Apr|23:30";s:2:"80";s:12:"33 Apr|23:30";s:2:"67";s:12:"34 r|23:30 ";s:2:"45";s:12:"35 Ap|23:30 ";s:2:"66";s:12:"34 Lr|23:30 ";s:2:"23";s:12:"3 Apr|23:30 ";s:2:"23";}'
...is not a valid serialization. Specifically, the s:12:"34 r|23:30 "; segment indicates that the string 34 r|23:30 contains 12 characters, which it does not.
$a = serialize($data);
$x = unserialize($a);

Parsing a malformed CSV file

How can I parse a CSV like this one in PHP (there's a double quote near value 8)?
"03720108";"value 8"";"";"219";"03720108";"value";"value";"value";"";"";"";"";"";"";"value";"";"";"value";"value";
I tried with fgetscv($pointer, 4096, ';', '"');
Your data seems to be malformed starting at the prior line. You have an opening quote with no closing quote.
Yes. Notice the extra quote.
"value 8"";
You might still be able to parse this string, though:
$str = '"03720108";"value 8"";"";"219";"03720108";"value";"value";"value";"";"";"";"";"";"";"value";"";"";"value";"value";';
$parsed = array_map(
function( $str ) { return substr($str, 1, -1); },
explode(';', $str)
);
var_export($parsed);
/*
array (
0 => '03720108',
1 => 'value 8"',
2 => '',
3 => '219',
4 => '03720108',
5 => 'value',
6 => 'value',
7 => 'value',
8 => '',
9 => '',
10 => '',
11 => '',
12 => '',
13 => '',
14 => 'value',
15 => '',
16 => '',
17 => 'value',
18 => 'value',
19 => false,
)
*/
Things get a bit more complicated, though, if there are elements that contain a ; character (normally would be escaped by enclosing the value in quotes... d'oh!), and the above code assumes that you only need to parse a single line (though if you are using fgets() to read the input stream, you should be OK).

PHP CodeIgniter Batch Insert Not Accepting My Array

I'm unable to get the following code to work, and it has something to do with the forming of the array. The array is actually build after a foreach() loop runs a few times, then I want to batch insert, but it comes up malformed. Why?
foreach ($results as $r) {
$insert_array = array(
'ListingRid' => $r['ListingRid'],
'StreetNumber' => $r['StreetNumber'],
'StreetName' => $r['StreetName'],
'City' => $r['City'],
'State' => $r['State'],
'ZipCode' => $r['ZipCode'],
'PropertyType' => $r['PropertyType'],
'Bedrooms' => $r['Bedrooms'],
'Bathrooms' => $r['Bathrooms'],
'YearBuilt' => (2011 - $r['Age']),
'Status' => $r['Status'],
'PictureCount' => $r['PictureCount'],
'SchoolDistrict' => $r['SchoolDistrict'],
'ListedSince' => $r['EntryDate'],
'MarketingRemarks' => $r['MarketingRemarks'],
'PhotoUrl' => (!empty($photo_url) ? $photo_url : 'DEFAULT'),
'ListingAgentFirstName' => $r['ListingAgentFirstName'],
'ListingAgentLastName' => $r['ListingAgentLastName'],
'ContactPhoneAreaCode1' => (!empty($a['ContactPhoneAreaCode1']) ? $a['ContactPhoneAreaCode1'] : 'DEFAULT'),
'ContactPhoneNumber1' => (!empty($a['ContactPhoneNumber1']) ? $a['ContactPhoneNumber1'] : 'DEFAULT'),
'ListingOfficeName' => (!empty($r['ListingOfficeName']) ? $r['ListingOfficeName'] : 'DEFAULT'),
'OfficePhoneComplete' => (!empty($o['OfficePhoneComplete']) ? $o['OfficePhoneComplete'] : 'DEFAULT'),
'last_updated' => date('Y-m-d H:i:s')
);
$insert[] = $insert_array;
}
$this->db->insert_batch('listings', $insert);
Here's the errors:
A PHP Error was encountered
Severity: Warning
Message: array_keys() [function.array-keys]: The first argument should
be an array
Filename: database/DB_active_rec.php
Line Number: 1148
A PHP Error was encountered
Severity: Warning
Message: sort() expects parameter 1 to be array, null given
Filename: database/DB_active_rec.php
Line Number: 1149
Any ideas? Thanks!
I've reduced your code to the bare minimum and it seems to work.
$i = 0;
while ($i <= 10) {
$insert_array = array(
'code' => 'asd'
);
$insert[] = $insert_array;
$i++;
}
$this->db->insert_batch('group', $insert);
You should check the elements of the array, comment them all and de-comment them one by one until you got the culprit.
Looks like you need to wrap your array in a second array. You're supposed to provide an array of row arrays.
$insert_array = array(array(...));

odd errors in php array

I am trying to json encode an array,it does encode but i get lots of errors:
$products = array( array( Title => "rose",
Price => "1.25,1.31,1.54,1.39",
Type => "dropdown"
),
array( Title => "daisy",
Price => "0.75",
Type => "text_field",
),
array( Title => "orchid",
Price => "1.15",
Type => "text_field"
)
);
echo json_encode($products);
I get the following errors.
Notice: Use of undefined constant Title - assumed 'Title' in C:\wamp\www\serializer.php on line 2
Notice: Use of undefined constant Price - assumed 'Price' in C:\wamp\www\serializer.php on line 3
Notice: Use of undefined constant Type - assumed 'Type' in C:\wamp\www\serializer.php on line 4
Notice: Use of undefined constant Title - assumed 'Title' in C:\wamp\www\serializer.php on line 6
Notice: Use of undefined constant Price - assumed 'Price' in C:\wamp\www\serializer.php on line 7
Notice: Use of undefined constant Type - assumed 'Type' in C:\wamp\www\serializer.php on line 8
Notice: Use of undefined constant Title - assumed 'Title' in C:\wamp\www\serializer.php on line 10
Notice: Use of undefined constant Price - assumed 'Price' in C:\wamp\www\serializer.php on line 11
Notice: Use of undefined constant Type - assumed 'Type' in C:\wamp\www\serializer.php on line 12
You need to quote the keys. Without quotes, they're constants. The interpreter is guessing what you mean, but you should change it to avoid the notice.
$products = array( array( "Title" => "rose",
"Price" => "1.25,1.31,1.54,1.39",
"Type" => "dropdown"
),
you must use quotation mark for STRING keys in arrays. Your code with changes is shown below:
<?php $products = array( array( 'Title' => "rose",
'Price' => "1.25,1.31,1.54,1.39",
'Type' => "dropdown"
),
array( 'Title' => "daisy",
'Price' => "0.75",
'Type' => "text_field",
),
array( 'Title' => "orchid",
'Price' => "1.15",
'Type' => "text_field"
)
); echo json_encode($products);
Additional information about arrays in php you will find here PHP: Arrays
put quotes around the array key names
$products = array( array( 'Title' => "rose",
'Price' => "1.25,1.31,1.54,1.39",
'Type' => "dropdown"
),
array( 'Title' => "daisy",
'Price' => "0.75",
'Type' => "text_field",
),
array( 'Title' => "orchid",
'Price' => "1.15",
'Type' => "text_field"
)
);
echo json_encode($products);
array( array( 'Title' => "rose",
'Price' => "1.25,1.31,1.54,1.39",
'Type' => "dropdown"
),
array( 'Title' => "daisy",
'Price' => "0.75",
'Type' => "text_field",
),
array( 'Title' => "orchid",
'Price' => "1.15",
'Type' => "text_field"
)
);
You might be confusing javascripts object notation syntax with PHP here, like the other answers have suggested, wrapping the array keys in quotes (so that they're passed in as strings) will sort out your problem.
It might be worth reading up on PHP Constants to better understand the error message you've been given: http://php.net/manual/en/language.constants.php
I came across the exact same problem and after staring at the PHP5 manual page for arrays it eventually clicked. Here is what I found out:
This line
If ($showallresult[Composer] == "") $showallresult[Composer] = "?";
will cause that notice to show.
I also use this line in my code
print ("<TD ALIGN=CENTER VALIGN=TOP>$showallresult[Composer]</TD>\n");
When I wrap the array key in single quotes in each line as such
If ($showallresult['Composer'] == "") $showallresult['Composer'] = "?";
print ("<TD ALIGN=CENTER VALIGN=TOP>$showallresult['Composer']</TD>\n");
I get a parse error on the second line, but the first line appears to be fine. Looking at http://us2.php.net/manual/en/function.array.php and there Example #4 the answer is right there. When accessing array values within a double quoted string you have to enclose the array value construct in curly braces (mustaches). As it turns out, this is how it is correct:
If ($showallresult['Composer'] == "") $showallresult['Composer'] = "?";
print ("<TD ALIGN=CENTER VALIGN=TOP>{$showallresult['Composer']}</TD>\n");
Oddly, this works as well without throwing parse errors or notices...welcome to the logic of PHP:
If ($showallresult['Composer'] == "") $showallresult['Composer'] = "?";
print ("<TD ALIGN=CENTER VALIGN=TOP>$showallresult[Composer]</TD>\n");
Rather bizarre that both lines work fine and generate the expected result. While the no-single-quote-no-curly-braces notation works I do suggest to go with how it seems to be correct and use curly braces and single quotes within a string.
In any case, reading the docs and mulling over it some time fixed it for me. And yes, sadly, it's all there right in the manual!

Categories