1

I have a XML and want to read values from only specific child nodes. Then I want to put those read values into an array and return this array at the end. So I want to get all values of name saved in an array and return this array. In the end, I want to display all values of this array beneath each other in an HTML file.

Here is my idea so far (which does not work obviously):

$items= simplexml_load_file('https://example.com/xml');
$itemList = array();
foreach ($items as $item->name) {
   // Push the values of name of the item nodes into an array
}
return $itemList;

Here is the structure of the XML file:

<item>
   <name>Name 1</name>
</item>
<item>
   <name>Name 2</name>
</item>
<item>
   <name>Name 3</name>
</item>
<item>
   <name>Name 4</name>
</item>
7
  • Please complete your question by describing how it doesn't work. Are you displaying anything? Nothing? Errors? Warnings? Notices? Commented Jul 22, 2019 at 12:16
  • Edited. I want do display the values in a HTML file. But in the end, this does a parser which is the reason why I have to return sth. Commented Jul 22, 2019 at 12:30
  • If my solution doesn't work please let me know so that I can adjust. Commented Jul 22, 2019 at 12:32
  • Would be great if it is possible to only return the values like Name 1, Name 2 etc. beneath each other. Commented Jul 22, 2019 at 12:53
  • So just echo impode("<br>", $names); then. This is super basic programming. Commented Jul 22, 2019 at 12:55

4 Answers 4

3

Iterate over each item and take it's name property. Explicitly cast name property to string, cause by default $item->name is not a string:

foreach ($items as $item) {
   $itemList[] = (string)$item->name;
}
Sign up to request clarification or add additional context in comments.

3 Comments

And how do I return this array or the values of this array? When I do return $itemList nothing gets displayed. Yes, I have to because I it has to work in a specific parser (Statamic).
What do you mean by "return"? You could of course simply return $itemList. If you need data exchange with a different language or system, you might want to convert this array into a JSON string: echo json_encode($itemList);?
return is used inside a function usually. I don't see function in your code. If there's a fuction - just use return $itemList; as the last line of your function.
2

You need to change foreach() code as well as do assignment inside it:-

$itemList = array();
foreach ($items as $item) { //$item->name will not work here
   $itemList[] = $item->name;
}

Comments

1

You can use json_encode() and json_decode() for converting xml data to string data type. After that, which tag you want to use, you can use this tag in foreach() loop. And after that, you should to create an empty array and insert all string data to empty array.

    $items = simplexml_load_file('https://example.com/xml');
    $json = json_encode($items);
    $array = json_decode($json,TRUE);

    $names = array();
    foreach ($array["item"] as $key => $value) {
      $names[] = $value["name"];
    }


    print_r($names);

Result:

  Array
(
    [0] => Name 1
    [1] => Name 2
    [2] => Name 3
    [3] => Name 4
)

Comments

0

I don't do a lot of xml processing, so I tried the earlier posted answers (and the answer that came after I posted) with your sample xml and they didn't work for me.

I managed to process your data by writing your xml inside of a parent element before parsing, then looping and casting the name as a string.

Code: (Demo)

$xml = <<<XML
<item>
   <name>Name 1</name>
</item>
<item>
   <name>Name 2</name>
</item>
<item>
   <name>Name 3</name>
</item>
<item>
   <name>Name 4</name>
</item>
XML;
foreach (simplexml_load_string("<root>{$xml}</root>")->item as $item) {
   $names[] = (string)$item->name;
}
var_export($names);

Output:

array (
  0 => 'Name 1',
  1 => 'Name 2',
  2 => 'Name 3',
  3 => 'Name 4',
)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.