0

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

1
  • Just above this code as I am combining two array. $contactlist = array_combine( $cntctnum, $cntcttype ); Commented Oct 24, 2011 at 7:45

2 Answers 2

6

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

Sign up to request clarification or add additional context in comments.

Comments

5

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."

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.