There must be an obvious bug in this code but I'm not seeing it. Mind taking a look?
The below code returns
string
fleet
Warning: Illegal offset type (line 6)
The taskforces subroutine just pulls an .ini file, reads it into an array, and returns the array, which the foreach then iterates through. In relevant part, the array looks like this.
; this is an INI file
[scout]
type = "fleet"
Here is the code:
foreach($_SESSION['ini']->taskforces() as $key => $val)
{
echo gettype($val["type"]);
echo $val["type"];
if($val["type"] == "fleet") {
$commanderData[$val] = "BLOB";
$commanderData["sc$val"] = "INT NOT NULL";
}
}
I'd like to not have the illegal offset type, because I want the code to go through to the if condition. What obvious thing am I missing?
Thanks.
Instead of this:
echo $val["type"];
you should have simply:
echo $val;
Just because $val is not an array, it's a string. You've made a foreach through an array, so on each iteration you get an array key and an array value (which is, obviously, the string "fleet").
I'm not sure why this caused the problem, but I realized that the result of the if statement was not correct. The code
$commanderData[$val] = "BLOB";
attempts to use the matrix $val as the key for the $commanderData array. It should use the string $key from the iteration through the ini file. Once fixed, I stopped getting the warning, but it's not clear why this would have thrown the error on the proceeding line.
Related
I have a very strange problem.
I am running through a foreach loop to compile an array but I receive an error.
I reveive the following warning :
Warning: Illegal string offset 'clientaccount_id' in
For this line of code:
$this->PreparedData[$table][$field] = 0;
I would say this to be logic if I would be doing something like:
$testVariable = $this->PreparedData[$table][$field];
Then the variable $field filled with 'clientaccount_id' would not exist.
But I am CREATING the field 'clientaccount_id' so to ME this is almost impossible to give an error.
The code
private function AssignData(){
foreach($this->FieldKeys as $table => $value){
///######## IF THE PREPARED DATA ARRAY DOES NOT EXIST
if(isset($this->PreparedData[$table]) === false){
///######## SET THE ARRAY KEY
$this->PreparedData[$table] = array();
}
///######## RUN THROUGH ALL SET SUB DATA
foreach($value as $field){
///######## IF THE FIELD EXISTS
if(isset($this->AccountData[$field]) === true){
///######## ASSIGN THE DATA
///$this->PreparedData[$table][$field] = $this->AccountData[$field];
///$this->PreparedData[$field] = $this->AccountData[$field];
$this->PreparedData[$table][$field] = 0;
}
}
}
exit('GOT THROUGH!!');
}
Could anyone see the error I am overlooking?
Solved!!
Thanks to VMcreator
Changed :
isset($this->PreparedData[$table]) === false
to this:
is_array($this->PreparedData[$table]) === false
Please read the explanation below WHY
Try to change this line:
isset($this->PreparedData[$table]) === false
to this:
!is_array($this->PreparedData[$table])
I saw this explanation here:
It just boils down to PHP's crazy type system.
$fruits['response']['errormessage'] is the string 'banana', so you're
attempting to access a character in that string by the ['orange']
index.
The string 'orange' is converted to an integer for the purposes of
indexing, so it becomes 0, as in
$fruits['response']['errormessage'][0]. The 0th index of a string is
the first character of the string, so for non-empty strings it's
essentially set. Thus isset() returns true.
You might be curious why your situation is comparable to that quoted statement even if $this->PreparedData[$table] seems a single dimensional array only, well its not a single dimensional array only, because you are accessing a class object, its just like doing this $this["PreparedData"][$table].
Update: It appears, upon further experimentation, that the problem is with $val. It may have been more helpful at describing the problem had I copied the full text of the error message: "Catchable fatal error: Object of class mysqli_result could not be converted to string in ...". Evidently, PHP (for some reason) can't convert $val to a string. Note that the error message states that it is a catchable error. I had hoped that the use of the "continue" parameter would bypass this error.
The code below works provided that none of the variables are themselves an array.
foreach ($_SESSION as $key=>$val)
{echo $key. ": ".$val. "<br>";}
Through experimentation, I figured out which "$key" was itself an array. Since I figured out which variable was causing the problem, the easy solution was simply to unset it. But that is not a good solution.
unset($_SESSION['issuenum_index']);
To get around the error, I attempted to use the "continue" statement. It did not work. See the code below.
foreach ($_SESSION as $key=>$val)
{if(is_array($key)) continue;
{echo $key. ": ".$val. "<br>";}}
How can the code above be revised to skip a variable when it is an array without triggering an error message?
Update continued: The "foreach" code failed and generated the following messages when the iteration reached the $key index "issuenum_index". Note that this "foreach" code correctly identifies the "$key" index as "issuenum_index" but hangs at this part of the code: "var_dump($val);"
foreach ($_SESSION as $key=>$val)
{ var_dump($key);
var_dump($val);
echo "<br>";
}
"Warning: var_dump(): Couldn't fetch mysqli_result in ..."
*Warning: var_dump(): Property access is not allowed yet in /var/www/sfmags/testform.inc.php on line 60
object(mysqli_result)#1 (5) { ["current_field"]=> NULL ["field_count"]=> NULL ["lengths"]=> NULL ["num_rows"]=> NULL ["type"]=> NULL } *
I have a solution that answers the question as posted. Essentially the variable $val associated with ($_SESSION['issuenum_index']) is an OBJECT.
In reading up on this, I found the following code snippet, that I applied to my case.
foreach ($_SESSION as $value) {
echo gettype($value), "\n";
}
The result: "integer string object integer integer integer integer". Which disclosed that one of my elements was an "object". The third element is the variable: $_SESSION['issuenum_index'].
I revised the original code to add "not object".
foreach ($_SESSION as $key=>$val)
if(!is_object($val))
{
echo $key. ": ".$val. "<br>";
} else {
echo "$key is an object <br>";
}
The code worked. I also revised the code to identify (echo) that "$_SESSION['issuenum_index']" was an object. This revision provides me a list of all my $_Session variables. My understanding of how this all goes together is still incomplete. I don't know why it works with $val? But that is another question.
$key, at one point in cycling through "$_SESSION" stands for "issuenum_index"
$val, I believed represented a value, such as "1". However, it is reported as being an "object". While this question is solved, I will still need to learn more.
I've got an odd error in my PHP code regarding dynamic arrays.
The error outputted is:
Fatal error: Cannot use string offset as an array ... on line 89
This is a portion of my code, it is within a foreach loop, which is looping through settings in a database:
foreach($query->fetchAll() as $row)
{
if($site!=CURRENT_SITE_TEMPLATE)
{
$property = 'foreignSettings';
$propertyType = 'foreignSettingsTypes';
} else {
$property = 'settings';
$propertyType = 'settingTypes';
}
$this->$property[$row['variable_section']][$row['variable_name']] = $row['variable_value'];
settype($this->$property[$row['variable_section']][$row['variable_name']],$row['variable_type']);
$this->$propertyType[$row['variable_section']][$row['variable_name']] = $row['variable_type'];
}
For the sake of the example code, $site is 'admin' and CURRENT_SITE_TEMPLATE is 'admin'.
In addition, $foreignSettings, $foreignSettingsTypes, $settings, and $settingTypes are all defined as arrays in the class scope
The error is on line 89, which is:
$this->$property[$row['variable_section']][$row['variable_name']] = $row['variable_value'];
I originally thought it was because of the $property variable accesing the array, however, this looks like valid legal code in the PHP documentation ( http://php.net/manual/en/language.variables.variable.php in example #1)
Any help on this error would be appreciated.
Thanks
In your given example $property is a string. You are then trying to use that as an array. Strings only has numeric indexes (if you need to use as an array).
The problem is as follows: $this->$property[0] means you access the 0th place of $property which in your case would be the first letter of the string $property. Thus you end up with $this->f or $this->s.
with $this->$property[0][0] you would be trying to access the 0th place of the 0th place of the $property string what results in an error because you try to access the 0th place of the char s what is not possible since the char s can not be referenced as an array.
what you want is $this->{$propperty}[0][0] what means that you try to access the 0th place of the 0th place of the variable that has the name $propperty.
I am trying to check array using another loop.
for( $i=0;$i<count($allCheckBoxId);$i++ ) {
if( $allCheckBoxId[$i] != ''){
unset( $contactlist[$cntctnum] );
}
}
I have named $allCheckBoxId, having contactnumber as value. Second array I have named $contactlist having contactnumber as key element.
For particular condition I am retrieving values of first array. Means I would have contactnumber as value retrieve in first array. IF it is not null I am unsetting second element with value contactnumber. but its giving me error on unset( $contactlist[$cntctnum] ); as Illegal offset type in unset in
Here comes interesting part.
You know, programming is not just writing the code.
Most of time programming is looking for errors. Not by asking questions on stackoverflow, but by amending your code and studying it's output and error messages. Some sort of investigation.
If you have got such an error message, doesn't that mean that somewhing wrong with offset type? Why not to print the problem variable out? just print it out:
for( $i=0;$i<count($allCheckBoxId);$i++ ) {
var_dump($cntctnum);
var_dump($allCheckBoxId[$i]);
var_dump($contactlist[$cntctnum]);
if( $allCheckBoxId[$i] != ''){
unset( $contactlist[$cntctnum] );
}
}
and see what's particularly wrong with your offset
Try casting your key into a string. Replace:
$contactlist[$cntctnum]
With
$contactlist[(string) $cntctnum]
OR
for($i = 0; $i < count($allCheckBoxId); $i++) {
if($allCheckBoxId[$i] != '') {
$key = (string) $cntctnum;
unset( $contactlist[$key] );
}
}
PHP associative arrays, as of PHP 5.4 will issue a PHP Warning: Illegal Offset Type if you use something other than a string as a key.
Furthermore, if this doesn't help, head over to the PHP Array Manual and do a Ctrl/Cmd + F for "Illegal Offset Type."
I am just doing some experimenting. How do I print the value of this $chvalues[] or test (as below) it's an array. Is it possible to print this out as-is? $chvalues[] (yes my input field is properly defined with name=seminar etc...)
$chvalues = array();
if (isset($_POST['seminar'])) {
foreach ($_POST['seminar'] as $ch => $value) {
$chvalues[] = $value;
if(is_array('$chvalues[]')) {
echo "Yes. It's an Array.";
}
}
}
I am trying to test this:
if(is_array('$chvalues[]')) { ...
I get this:
Fatal error: Cannot use [] for reading in...
You are reading the array as a string by using single quotes around it (which should just return false).
You should use if(is_array($chvalues)) without the brackets.
$chvalues will always be an array, because you just assigned it to one the line before, as well as at the start of your code, before the loop.
Maybe you meant to check $value, or run the is_array() after the loop and not initialize $chvalues as an array before running the loop.
Following a variable by [] is only for appending new elements to the end of the array within the variable. If you want to look at the array then just use the variable itself.
if(is_array($chvalues)) {
...
You only use $value[] for accessing the "top" element of the $value array, the unset next position, if you will.
Change it to $chvalues in the is_array() function.
To print an array, it depends on output. Check print_r for debugging.
Also don't use quotes as they make the array act like a string, in this case (because they are single quotes) the string '$chvalues[]' is being tested for being an array.
Two problems here. First, everytime you do $chvalues[] with empty brackets, you are appending a new empty element onto your array. So when you do:
if(is_array($chvalues[]))
You always get an empty element, never an array.
Instead you probably want:
if(is_array($chvalues))
Second problem is you cannot single-quote '$chvalues[]' as you have done. It should have no quotes as in my first code block above.
don't use $chvalues[], just $chvalues (without single quotes inside the is_array function).
$chvalues = array();
if (isset($_POST['seminar'])) {
foreach ($_POST['seminar'] as $ch => $value) {
$chvalues = $value;
if(is_array($chvalues)) {
echo "Yes. It's an Array.";
}
}
}
You really want, is_array($chvalues) (which will always be true in your code) or, if you're trying to find out if the last item added to $chvalues is an array, you should call is_array($value). If you don't have that option (not sure why you wouldn't), then you can use is_array(end($chvalues)) to test the last thing added to the array. I will warn, though, that you will need to call reset to iterate through $chvalues with foreach.
Of course, you could also call $arr = array_keys( $chvalues ); is_array($chvalues[end($arr)]);, but that gets very silly (especially since I left out the last paren originally).