tags:

views:

56

answers:

2

Working with the simplexml_load_file() function, I get a side effect. To understand what I mean, see this code:

$result = simplexml_load_file($xml_link);

$arr = array();
foreach ($result->element as $elem) {
    $arr[] = $elem->number[0];
}

print_r($arr);

Output:

Array
(
[0] => SimpleXMLElement Object
    (
        [0] => 330411879136
    )

[1] => SimpleXMLElement Object
    (
        [0] => 370346266228
    )

[2] => SimpleXMLElement Object
    (
        [0] => 370346266223
    )
)

How would I store data into the array so that output would look like so:

 Array
 (
 [0] => 330411879136

 [1] => 370346266228

 [2] => 370346266223
 )
+1  A: 

You have to convert the data you get from SimpleXML to the PHP datatype that interests you.

For instance, here, you should use something like this :

$result = simplexml_load_file($xml_link);
$arr = array();
foreach ($result->element as $elem) {
    $arr[] = intval($elem->number[0]);
}

i.e. here, we "force" the conversion to integers.


In the case of integers, this can also be done using a type-cast :

$result = simplexml_load_file($xml_link);
$arr = array();
foreach ($result->element as $elem) {
    $arr[] = (int)$elem->number[0];
}

Same for many other types, btw -- see the Type Casting section, in the manual.


For instance, you could use something like this for strings :

$arr[] = (string)$elem->number[0];

Or like that for floats :

$arr[] = (float)$elem->number[0];
Pascal MARTIN
A: 

Please, could you tell me how to convert to strings for that matter?

Kole Odd