0

I am writing an API to output an adress book, which I get the output as a XML. The output is JSON.


What's my problem?

I looping through the entries, which I get from an XML file and store it in an array. That means, that every entry includes an contact:

$xml = new SimpleXMLElement("adressbook.xml"); // <-- Only an example URL
$totalResults = $xml->children('openSearch', true)->totalResults;

$contacts=array();

foreach($xml->entry as $contact){
  $tel = $contact->children('tel', true);
  $entry = array(
    "type"=>$tel->type,
    "name"=>$tel->name,
    "firstname"=>$tel->firstname,
    "street"=>$tel->street,
    "streetno"=>$tel->streetno,
    "zip"=>$tel->zip,
    "city"=>$tel->city,
    "canton"=>$tel->canton,
    "country"=>$tel->country,
    "phone"=>$tel->phone
  );
  array_push($contacts,$entry);
}

After this I want to echo the array in json. I do this with json_encode. But there is the problem. Instead of giving the results directly, it shows me following results:

Output of JSON

The zero's which are red marked should not exist.

What I tried

I researched in the internet and found in the docs and in a few posts on Stack Overflow the JSON_FORCE_OBJECT attribute for json_encode. http://php.net/manual/en/function.json-encode.php

Now my function looks like this:

echo json_encode($contacts,JSON_FORCE_OBJECT);

But I still get the zero's.

Here is also a small example of my XML:

<entry>
    <updated>2017-04-11T02:00:00Z</updated>
    <published>2017-04-11T02:00:00Z</published>
    <title type="text">Mustermann, Max</title>
    <tel:type>Person</tel:type>
    <tel:name>Mustermann</tel:name>
    <tel:firstname>Max</tel:firstname>
    <tel:street>Musterstrasse</tel:street>
    <tel:streetno>17</tel:streetno>
    <tel:zip>8000</tel:zip>
    <tel:city>Zürich</tel:city>
    <tel:canton>ZH</tel:canton>
    <tel:country>ch</tel:country>
    <tel:phone>+123456789</tel:phone>
</entry>

What is going wrong?

UPDATE:

Here is the var_dump:

object(SimpleXMLElement)#6 (15) { ["type"]=> string(12) "Organisation" ["name"]=> string(28) "*******" ["occupation"]=> string(57) "*******" ["street"]=> string(11) "Musterstrasse" ["streetno"]=> string(1) "17" ["zip"]=> string(4) "8000" ["city"]=> string(5) "Zürich" ["canton"]=> string(2) "ZH" ["country"]=> string(2) "ch" ["phone"]=> string(12) "+123456789" }
Julian Schmuckli
  • 3,681
  • 11
  • 37
  • 64

1 Answers1

2

One approach is to use strval to convert each object to a string

$xml = new SimpleXMLElement("adressbook.xml"); // <-- Only an example URL
$totalResults = $xml->children('openSearch', true)->totalResults;

$contacts=array();

foreach($xml->entry as $contact){
  $tel = $contact->children('tel', true);
  $entry = array(
    "type"=>strval($tel->type),
    "name"=>strval($tel->name),
    "firstname"=>strval($tel->firstname,)
    "street"=>strval($tel->street),
    "streetno"=>strval($tel->streetno),
    "zip"=>strval($tel->zip),
    "city"=>strval($tel->city),
    "canton"=>strval($tel->canton),
    "country"=>strval($tel->country),
    "phone"=>strval($tel->phone)
  );
  array_push($contacts,$entry);
}
Michael Mior
  • 28,107
  • 9
  • 89
  • 113