Illegal offset type for array. Object to integer convertion

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Manish

    Illegal offset type for array. Object to integer convertion

    The code is as ...


    $folderlistfile = $path."xmlfile. xml";

    /*

    <list>
    <details id="2">
    <name>Books</name>
    <mainid>0</mainid>
    </details>
    <details id="1003">
    <name>CDs</name>
    <mainid>0</mainid>
    </details>
    </list>

    */

    $list = simplexml_load_ file($folderlis tfile);

    foreach($list as $sno=>$details) {
    if($details->isdeletable) {
    if($details->mainid) {

    echo "<br /mainid = ".$details->mainid." : id =
    ".$details['id']." ";
    var_dump($detai ls->mainid);
    $mainid = $details->mainid;
    settype($mainid , "integer");
    var_dump($maini d);

    // I want to save the details in an array
    //$listarr[$mainid][] = $details['id']; //Warning: Illegal offset
    type
    //$listarr[$details->mainid][] = $details['id']; //Warning: Illegal
    offset type
    }
    }
    }


    echo, var_dump will output as follow ...

    mainid = 0 : id = 2
    object(SimpleXM LElement)#5 (1) { [0]= string(1) "0" }
    UNKNOWN:0


    mainid = 0 : id = 1003
    object(SimpleXM LElement)#4 (1) { [0]= string(1) "0" }
    UNKNOWN:0


    Both of this generate the error : Warning: Illegal offset type

    $listarr[$mainid][] = $details['id']; //Warning: Illegal offset type
    $listarr[$details->mainid][] = $details['id']; //Warning: Illegal
    offset type

    and page doesn't execute furthur of this line.


    I suppose it is because the array key is an object. How do I convert it
    to integer.

    print_r($detail s->mainid) displays SimpleXMLElemen t Object ( [0] =0
    )
    print_r($mainid ) : nothing is displyed. even mainid = 0 : id = 2 is
    not displayed.


    System: free bsd, php 5

    As is mentioned in the code, I want to craete an array as

    either
    $listarr[$mainid][] = $details['id']; //Warning: Illegal
    offset type
    or
    $listarr[$details->mainid][] = $details['id']; //Warning:
    Illegal offset type


    Thanks.

    Manish

  • Manish

    #2
    Re: Illegal offset type for array. Object to integer convertion


    Got solved the problem by casting the variable.

    (string) $variable;



    Manish wrote:
    The code is as ...
    >
    >
    $folderlistfile = $path."xmlfile. xml";
    >
    /*
    >
    <list>
    <details id="2">
    <name>Books</name>
    <mainid>0</mainid>
    </details>
    <details id="1003">
    <name>CDs</name>
    <mainid>0</mainid>
    </details>
    </list>
    >
    */
    >
    $list = simplexml_load_ file($folderlis tfile);
    >
    foreach($list as $sno=>$details) {
    if($details->isdeletable) {
    if($details->mainid) {
    >
    echo "<br /mainid = ".$details->mainid." : id =
    ".$details['id']." ";
    var_dump($detai ls->mainid);
    $mainid = $details->mainid;
    settype($mainid , "integer");
    var_dump($maini d);
    >
    // I want to save the details in an array
    //$listarr[$mainid][] = $details['id']; //Warning: Illegal offset
    type
    //$listarr[$details->mainid][] = $details['id']; //Warning: Illegal
    offset type
    }
    }
    }
    >
    >
    echo, var_dump will output as follow ...
    >
    mainid = 0 : id = 2
    object(SimpleXM LElement)#5 (1) { [0]= string(1) "0" }
    UNKNOWN:0
    >
    >
    mainid = 0 : id = 1003
    object(SimpleXM LElement)#4 (1) { [0]= string(1) "0" }
    UNKNOWN:0
    >
    >
    Both of this generate the error : Warning: Illegal offset type
    >
    $listarr[$mainid][] = $details['id']; //Warning: Illegal offset type
    $listarr[$details->mainid][] = $details['id']; //Warning: Illegal
    offset type
    >
    and page doesn't execute furthur of this line.
    >
    >
    I suppose it is because the array key is an object. How do I convert it
    to integer.
    >
    print_r($detail s->mainid) displays SimpleXMLElemen t Object ( [0] =0
    )
    print_r($mainid ) : nothing is displyed. even mainid = 0 : id = 2 is
    not displayed.
    >
    >
    System: free bsd, php 5
    >
    As is mentioned in the code, I want to craete an array as
    >
    either
    $listarr[$mainid][] = $details['id']; //Warning: Illegal
    offset type
    or
    $listarr[$details->mainid][] = $details['id']; //Warning:
    Illegal offset type
    >
    >
    Thanks.
    >
    Manish

    Comment

    Working...