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."
Related
I'm getting this warning on my website, im just getting the information is it IP available or not, it's work, its says it's unavailable but it shows an warning also about code, I searched about it, but none of those answers didn't help me. So the code is working, just want to get off this warning. Php 7.x doesn't support syntax like this? The problem is in if (count($test)==1).
$printers = file("printers.txt"); //input txt file with IP addresses in chosen order
$number_of_printers = count ($printers);
for ( $i = 0 ; $i < $number_of_printers ; $i++)
{
$ip=str_replace("\r\n","",$printers[$i]);
$ip=str_replace("\r","",$ip);
$ip=str_replace("\n","",$ip);
$test=#get_headers("http://$ip");
if (count($test)==1){
//do stuff here
echo "<div class='printerwrapper'<div class='location'>$ip is unavailable</div></div>";
continue;
}
When get_headers() fails it returns false and count(false) triggers that warning since PHP/7.2 (demo). The rationale is that counting a boolean false and getting 1 doesn't make much sense.
Perhaps you just want this:
if (!$test) {
//do stuff here
echo "<div class='printerwrapper'<div class='location'>$ip is unavailable</div></div>";
continue;
}
get_headers($ip) provides you an array if the $ip is exist, otherwise it returns FALSE. So you should check the return value of get_headers() weather it is an array or not. An example:
$headerInfo = #get_headers("http://127.0.0.1");
if (is_array($headerInfo) && count($headerInfo)) {
// do something
} else {
// do others
}
Note: PHP count demands the first argument an array or countable object.
I get a website as a cURL response, and I pass it to this function. Then I itterate through the string, doing some processing. It works, but I get this error:
Notice: Uninitialized string offset: 75817 in
/var/www/html/wp-content/plugins/crg-daily/lib/CragslistRawDumpProcessor.class.php
on line 7
Here is the code:
public function rollSausage($dump){
$anchorArray = array();
$dumpLength = strlen($dump);
$skipToLetter = 1;
while($skipToLetter < $dumpLength){
$skipToLetter++;
$letter = $dump[$skipToLetter];
...
}
}
Any ideas? I think it has something to do with the type of string being submitted. It is a raw cUrl response. I'm scraping a web page.
Increment your $skipToLetter after you use it (preferably at the end of the while loop). And you might also start at 0, not 1
$skipToLetter = 0;
while($skipToLetter < $dumpLength){
$letter = $dump[$skipToLetter];
...
$skipToLetter++;
}
}
Here's the reason: assume you have a string with length of 4. This means that the last index in the string is 3. Your index goes up to 3. It gets compared in the while loop (3<4)? and the answer is true. The code enters the while loop and increments the value of the index which will be greater than the last index of the string, thus causing the warning.
Updated your code...
public function rollSausage($dump){
$anchorArray = array();
$dumpLength = strlen($dump);
$skipToLetter = 1;
while($skipToLetter < $dumpLength){
$skipToLetter++;
if( siset( $dump[$skipToLetter]) )
$letter = $dump[$skipToLetter];
...
}
}
}
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].
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.
I am getting this error: "Fatal error: Can't use function return value in write context in D:\Programas\wamp\www\away\index.php on line 18". Line 18 being the if statement.
Can anyone help me out on this? Thanks.
$vars = array("first_date_month", "first_date_day", "last_date_month", "last_date_day", "resume_date_month", "resume_date_day", "pay_date_month", "pay_date_day", "pay_time_hour", "pay_time_minutes");
$err_flag = false;
$i = 0;
while ($i < count($vars) and $err_flag == false)
{
if ( (!isset($_GET($vars[$i])) or ($_GET[$vars[$i] == "0") )
$err_flag = true;
$i++;
}
Maybe I'm not seeing well, but:
if ( (!isset($_GET($vars[$i])) or ($_GET[$vars[$i] == "0") )
You got a really awful mixup of parenthesis and square brackets. There's no such thing as
$_GET()
Big typo you have to correct.
Your code is a mess.
$_GET is an associative array and not a function (you are using the function call syntax passing $vars[$i] as an argument). In the second $_GET there's one ] missing.
Line 18 should be:
if ( (!isset($_GET[$vars[$i]]) or ($_GET[$vars[$i]] == "0") )
My take at it:
$vars = array("first_date_month", "first_date_day", "last_date_month", "last_date_day", "resume_date_month", "resume_date_day", "pay_date_month", "pay_date_day", "pay_time_hour", "pay_time_minutes");
foreach ($vars as $var) {
if ($err_flag = empty($_GET[$var]))
break;
}
8)
I assume the marked answer has... well.. answered the problems in your code, so just throwing out some optimizations:
using foreach() instead of while/for
is simple in many cases where we are
just iterating over an array - we can get the value and also the key if needed.
using the empty() function (returns true for anything null, false, 0, "", "0")
exit the loop when you're done using break
$_GET is a variable, an array -- and not a function.
It means you have to use array-access, with [], to get the data it contains.
So :
$_GET[$vars[$i]]
instead of
$_GET($vars[$i])
for the first time you are using $_GET.
And, for the second time, you forgot to close one ] ; which means you need to use :
$_GET[$vars[$i]]
instead of
$_GET[$vars[$i]
In the end, you while-loop should look like this :
while ($i < count($vars) and $err_flag == false)
{
if ( !isset($_GET[$vars[$i]]) or $_GET[$vars[$i]] == "0" ) {
$err_flag = true;
}
$i++;
}
Note I also added {} arround the body of the if condition ; this way, if you ever have to add something, you won't risk forgetting those ;-)
Change your line 18 if... to:
if ( (!isset($vars[$i])) or ($vars[$i] == "0") )
$err_flag = true;
$i++;
This mainly because I -and this is purely personal, I suspect- don't like using the $_GET[...] throughout the script; assign:
$variable_from_GET[] = $_GET['variable_name'];
and then use the $variable_from_GET array variable in your conditions which you -I presume- you did since you had a $vars array.