CoderZone.org
Pages: 1 « previous     next »
  Print  
Author Topic: PHP Warning: Illegal offset type in isset or empty  (Read 40168 times) Bookmark and Share
phpMan2010
Newbie
*
Posts: 32



View Profile
« on: Mar 05, 2011, 10:39:24 am »

Why do I get this warning?

 Huh

Code:  
Highlight Mode: (PHP)
  1. <?php
  2.        $xml=file_get_contents('xml.xml');
  3.        $data = new SimpleXMLElement($xml);
  4.  
  5.        $aPost=array();
  6.        $aPost['url']='banshee.com';
  7.        $aPost['id']='zonk';
  8.  
  9.        $bValid=true;
  10.        foreach ($data->fields->field as $f)
  11.                if (isset($aPost[$f->name]))
  12.                {
  13.                        $f->value=$aPost[$f->name];
  14.                        if (!preg_match('/^'.$f->regex.'$/',$aPost[$f->name]))
  15.                        {
  16.                                $f->invalid=(bool)true;
  17.                                $bValid=false;
  18.                                echo $f->name.' is invalid!';
  19.                        }
  20.                }
  21. ?>
 


Code:  
Highlight Mode: (XML)
  1. <?xml version="1.0" encoding="utf-8" ?>
  2. <interface>
  3.        <name>Works</name>
  4.        <fields>
  5.                <field>
  6.                        <name>URL</name>
  7.                        <length>255</length>
  8.                        <validation>[\w\.\-]{2,255}</validation>
  9.                        <default>domain.com</default>
  10.                        <label>URL</label>
  11.                        <value>url.com</value>
  12.                        <required>true</required>
  13.                        <errortext>Letters, numbers, periods and dashes only</er
  14. rortext>
  15.                </field>
  16.                <field>
  17.                        <name>id</name>
  18.                        <length>11</length>
  19.                        <validation>[\d]{1,11}</validation>
  20.                        <default>1</default>
  21.                        <label>Id</label>
  22.                        <required>true</required>
  23.                        <errortext>Ids must be all digits</errortext>
  24.                </field>
  25.        </fields>
  26. </interface>
 
« Last Edit: Mar 05, 2011, 10:45:56 am by phpMan2010 » Logged
Max
Jr. Member
*****
Posts: 75



View Profile WWW
« Reply #1 on: Mar 05, 2011, 11:15:54 am »

Hmmmm...not sure.

I think it may have something to do with trying to index an array with something that isn't an integer or possibly trying to index something that wasn't actually an array. I've seen this before but don't remember the exact cause or the fix.

For what it's worth, php.net has this to say:

"Arrays and objects can not be used as keys. Doing so will result in a warning: Illegal offset type."

http://php.net/manual/en/language.types.array.php
Logged
cuberat
Newbie
*
Posts: 40


View Profile
« Reply #2 on: Mar 08, 2011, 06:31:21 am »

try using the string parsing ...

http://www.php.net/manual/en/language.types.string.php#language.types.string.parsing
Logged
phpMan2010
Newbie
*
Posts: 32



View Profile
« Reply #3 on: Mar 08, 2011, 02:45:52 pm »

Yup - this worked ...

Code:  
Highlight Mode: (PHP)
  1. $aPost["{$f->name}"]
 

Logged
Tags:
Pages: 1
  Print  
 
Jump to: