How to fix it?
Notice: Trying to access array offset on value of type int in
C:\xampp\htdocs\SPP2\petugas\admin\kelas\id_kelas.php on line
12
This is the code:
$nilaikode = substr($jumlah_data[0], 1);
As others in the comments have noted, the value of $jumlah_data in this case is an integer, but it's being used as an array.
You'll likely want to trace back to where $jumlah_data is set and use something like var_dump($jumlah_data) to see what the value is and determine if it's what you expect.
Then either fix the way the variable is set so it is an array, or modify your code so that it treats the value as an integer - e.g.
$nilaikode = substr($jumlah_data, 1);
Related
I have a variable in session that contains array of numbers. I want to change a specific number inside that variable.
My session:
$user_data = array(
'user_id' => $user_id,
'username' => $username,
'logged_in' => true,
// 20 slots, the counting starts from 0, the last slot's position
// is 19.
'slots_id' => array(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0),
);
So, slots_id is the variable that i want to access. I need to change the last number, it.In my controller i have the following.
$this->session->set_userdata('slots_id'[19], 100);
Here i'm trying to set the last slot's value to 100. But the "[19]" that i put there causes error.
A PHP Error was encountered Severity: Notice Message: Uninitialized string offset: 19
Google did not help me in my current situation, thank you for your time.
Two ways around your problem:
$this->session->set_userdata('slots_id'[19], 100); is not attempting to assign a value of 100 to the array element on index 19 for the $slots_id array. It's trying to set a value of 100 as the 20th character (index 19) of the 'slots_id' string, which goes out of bounds.
You may try:
$this->session->set_userdata('slots_id[19]', 100);
Or even better, remove the full element from session, update it and reset it:
$aux = $this->session->userdata('slots_id');
$aux[19] = 100;
$this->session->unset_userdata('slots_id');
$this->session->set_userdata('slots_id', $aux);
it's a bit more code, but my advice would be to always replace session data rather than update it on the fly.
Uninitialized string offset errors in PHP have a vast amount of documentation around them. You are getting this error because you are not referencing index 19 of an Array, you're referencing it of a string, which is exactly what 'slots_id' is in this instance. 'slots_id' is 8 characters long which means any index beyond 7 is in fact undefined.
If you have no direct access to the $_SESSION superglobal for some odd reason, you need to retrieve the entire slots_id array via the helper, update the index you need in your local copy, then update the session variable's slots_id array with your updated copy.
PHP is unable to get the value for dynamic object prepared as:
$abc->{$dynamic_object_pattern}
Where the value of the variable $dynamic_object_pattern is, json->{'data_1'}->{'value'}
For me, PHP 7.1 is understanding the statically defined pattern like below, and fetching the value as it should:
$abc->json->{'data_1'}->{'value'}
But not when I put the whole portion into a variable and then try to get its value. I Tried,
$abc->{$dynamic_object_pattern} and $abc->$dynamic_object_pattern
both ways, but no solution yet.
The error comes is Notice: Undefined property: stdClass::$json->{'data_1'}->{'value'}
I'm attempting an answer without seeing your JSON data
Here you say :
But not when I put the whole portion into a variable and then try to
get its value
From that line alone it sounds like you are trying to get value from a string rather than array. If you put the whole portion into a variable, PHP will interpret it as string. Make sure you add array() before newly created variable.
Natural array :
$array = array();
Now a string
$variable = $array;
Convert string to array
$new_array = array($variable);
Also, have you tried decoding?
// decode
$response = json_decode($new_array, true);
//print out
var_export(array_unique(array($response)));
$q2=$_REQUEST['binge'];
'book2'=>array('callno'=>123006,'price'=>number_format(844,2),'desc'=>'Binge','auth'=>'Tyler Oakley','quant'=>$q2,'total'=>number_format(844,2)*$q2)
On this particular code, It kept displaying errors like this
Warning: A non-numeric value encountered in C:\xampp\htdocs\Webcard_3new\Webcard\wishlist.php on line 97
I searched all over the net for finding the right answers but some are just so complex to understand...
It supposed to be that $q2 is the variable inside an array. That variable is then multiplied to the "TOTAL". but the errors kept on going.. please help!!
The super-globals will always be strings. You need to explicitly convert them using intval():
$q2 = intval($_REQUEST['binge']);
Also, this line:
'book2'=>array...
Should be
$book2 = array...
You can use
$q2 = filter_var($_REQUEST['binge'], FILTER_VALIDATE_INT);
here you will have the benefit of validation where false is returned when someone passes a value that is not an integer. If it is instead a float use FILTER_VALIDATE_FLOAT instead.
Also, consider using $_GET, or $_POST directly to have more control of the data channel. $_REQUEST cobbles together several things into one, which sometimes may cause issues when more than one channel have the same key.
The easiest way for me to explain this is to show an example ... Here's a replication of the problem code:
<?php
$test=array();
$test['one']='hello';
if(isset($test['one']['two'][0])) {
echo 'Apparently it is set ...';
echo $test['one']['two'][0];
}
?>
This returns as:
Apparently it is set ...
Warning: Illegal string offset 'two' in C:\test.php on line 6
h
Is this because there are mixed key types? It's just a little anomaly I came across and was wondering if someone could shed some light on it ...
The reason is that, when you dereference a string, it will return a string comprising a single character (assuming the index doesn't exceed the length); the resulting string can be dereferenced again (starting from 5.4 onwards).
For example - link:
$s = 'hello';
$s[0]; // "h"
$s[0][0]; // "h"
// etc. etc.
Illegal indices such as 'two' will cause a notice but it's treated as index 0, except when used inside isset().
Another example:
$s[0][1]; // ""
$s[0][1][0]; // notice: uninitialised string offset: 0
If you don't know beforehand whether a string or array is passed and this is important to you, additional type checks need to take place in between each path.
You should check your all your array keys exist before you try and use them, i.e. all the way up the chain. isset() accepts multiple parameters so you don't need to keep rewriting it and can keep DRY principles going a little more:
$test = array();
$test['one'] = 'hello';
if (isset($test['one'], $test['one']['two'], $test['one']['two'][0])) {
echo 'Apparently it is set ...';
echo $test['one']['two'][0];
}
isset returns odd and unexpected results when you pass it a string instead of an array.
It is good practice to pair an an is_array check with an isset check.
I have classes, each with a date related member variable that always has the same naming format - field_{$node->type}_date
For example, if I my node type was 'car', the date field would be named field_car_date
So I am looping over all my nodes and I want to access the date related field for each of them. However I am getting an error. Here's the code
$date_field_key = 'field_' . $node->type . '_date';
if (isset($node->$date_field_key['und'][0]['value'])) {
I get an error because of the second line. The error is - Illegal string offset 'und'
The date related variable is an array and it does have an element with the key 'und'. If I write out the line explicitly - $node->field_car_date['und'][0]['value'] - it works fine. It's just when I dynamically create the field name that I get this problem.
Any solution for this, is my syntax incorrect?
You need to surround you key value in {} because it's a dynamically-assigned variable.
In your second line, you have $node->$date_field_key['und'][0]['value'] where you should have:
$node->{$date_field_key}['und'][0]['value']
Notice the {} surrounding the date_field_key
Good luck!
There is no reason to spare the variable:
$array = $this->$date_field_key;
$value = $array['und'][0]['value'];
If you get it to work, we then can discuss more advanced topics.