By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,142 Members | 1,831 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,142 IT Pros & Developers. It's quick & easy.

Illegal offset type for array. Object to integer convertion

P: n/a
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($folderlistfile);

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

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

// 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(SimpleXMLElement)#5 (1) { [0]= string(1) "0" }
UNKNOWN:0
mainid = 0 : id = 1003
object(SimpleXMLElement)#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($details->mainid) displays SimpleXMLElement 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

Jul 27 '06 #1
Share this Question
Share on Google+
1 Reply


P: n/a

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($folderlistfile);

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

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

// 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(SimpleXMLElement)#5 (1) { [0]= string(1) "0" }
UNKNOWN:0
mainid = 0 : id = 1003
object(SimpleXMLElement)#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($details->mainid) displays SimpleXMLElement 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
Aug 7 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.