Output @attributes of array

I am looking for a way to display the attribut of an array. In this case I would like to check if a publication is locked with 'publications locked=“0” or ‘publications locked=“1”’

This is a shortened version of the XML-File

<?xml version="1.0" encoding="UTF-8"?>
<project>
  <item>
    <hash>07548882287dc5asdasd2dbf741639abce</hash>
    <ids label="IDs">
      <system_id label="System ID">5874123</system_id>
    </ids>
    <publications locked="0">
      <en>
        <title>English Projecttitle</title>
        <sample><![CDATA[<span style="color: #222222; font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 11px;">Sample English</span>]]></sample>
        <contacts>
          <contact>
            <type>
              <id>3525</id>
              <name>Print Owner</name>
            </type>
            <text><![CDATA[Sample Name, City]]></text>
          </contact>
          <contact>
            <type>
              <id>3526</id>
              <name>Sample Owner</name>
            </type>
            <text><![CDATA[Sample Name, City]]></text>
          </contact>
        </contacts>
      </en>
      <de>
        <title>Deutscher Projekttitel</title>
        <sample><![CDATA[<span style="color: #222222; font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 11px;">Sample Deutsch</span>]]></sample>
        <contacts>
          <contact>
            <type>
              <id>3525</id>
              <name>Print Owner</name>
            </type>
            <text><![CDATA[Sample Name, City]]></text>
          </contact>
          <contact>
            <type>
              <id>3526</id>
              <name>Sample Owner</name>
            </type>
            <text><![CDATA[Sample Name, City]]></text>
          </contact>
        </contacts>
      </de>
  </item>
</project>

This is the (working) model …

<?php

class FeedPage extends Page {

    public function children()
    {
        $results = [];
        $pages   = [];

        $request = Remote::get('sample.xml');

        if ($request->code() === 200) {
            $results = Xml::parse($request->content());
        }

        if (count($results) > 0) {
            foreach ($results['item'] as $item) {
                $pages[] = [
                    'slug'     => Str::slug($item['hash']),
                    'template' => 'feeditem',
                    'model'    => 'feeditem',
                    'content'  => [
                        'publications'   => $item['publications'] ?? '',
                    ]
                ];
            }
        }


        return Pages::factory($pages, $this);
    }

}

and this the template …

<?php foreach ($page->children()->sortBy('date', 'desc') as $item): ?>
  
  DISPLAY ATTRIBUTE

<?php endforeach ?>

I am grateful for any tips!

In the same way as any other array item by its key, in this case @attributes

I think it helps if you dump() your array to have a look at the structure, so that you then know how to access a particular item.

Thank you. I feel a bit silly now.

You shouldn’t. We all learn. Every day.

Maybe anyone finds to time to help me out. I am desperately trying to get certain values. Without luck so far. Here is the shortened XML-File with slightly different content. I can read out the @attribute label but I cannot get to the other values.

I would like to display the description in material. Here is the XML-File:

<?xml version="1.0" encoding="UTF-8"?>
<project>
  <item>
    <hash>07548882287dc5asdasd2dbf741639abce</hash>
    <ids label="IDs">
      <system_id label="System ID">5874123</system_id>
    </ids>
    <sample_info label="Film information">
      <materials label="Material">
        <material type="file" user_type="Stills" description="Sample description 1" file="sample-1.jpg"/>
        <material type="file" user_type="Stills" description="Sample description 2" file="sample-2.jpg"/>
        <material type="file" user_type="Stills" description="Sample description 3" file="sample-3.jpg"/>
        <material type="file" user_type="Stills" description="Sample description 4" file="sample-4.jpg"/>
        <material type="file" user_type="Stills" description="Sample description 5" file="sample-5.jpg"/>
      </materials>
    </sample_info>
  </item>
</project>

Here the model:

<?php

class FeedPage extends Page {

    public function children()
    {
        $results = [];
        $pages   = [];

        $request = Remote::get('sample.xml');

        if ($request->code() === 200) {
            $results = Xml::parse($request->content());
        }

        if (count($results) > 0) {
            foreach ($results['item'] as $item) {
                $pages[] = [
                    'slug'     => Str::slug($item['hash']),
                    'template' => 'feeditem',
                    'model'    => 'feeditem',
                    'content'  => [
                        'materials' => $item['sample_info']['materials'] ?? '',
                    ]
                ];
            }
        }


        return Pages::factory($pages, $this);
    }

}

And here the template. When I try to loop over with <?php foreach ($material['material'] as $material_item):?> I get Undefined index: material

<?php foreach ($page->children()->sortBy('date', 'desc') as $item): ?>
  
  <?php $materials = $item->materials()->toArray() ?>
  <?php if (isset($materials)): ?>
    <?php foreach ($materials as $material):?>
      <?php if (is_array($material)==true):?>
        <?= $material['@attributes']['label'] ?> //Display label "Material"
        <?php foreach ($material['material'] as $material_item):?> // Here I get "Undefined index: material"
          <?= $material_item['description']?>
        <?php endforeach ?>
      <?php endif ?>
    <?php endforeach ?>
  <?php endif ?>

<?php endforeach ?>

Any ideas?

What does this give you if you dump it?

I get the following …

array(1) {
  ["materials"]=>
  array(2) {
    ["@attributes"]=>
    array(1) {
      ["label"]=>
      string(8) "Material"
    }
    ["material"]=>
    array(5) {
      [0]=>
      array(2) {
        ["@attributes"]=>
        array(4) {
          ["type"]=>
          string(4) "file"
          ["user_type"]=>
          string(6) "Stills"
          ["description"]=>
          string(20) "Sample description 1"
          ["file"]=>
          string(12) "sample-1.jpg"
        }
        ["@value"]=>
        string(0) ""
      }
      [1]=>
      array(2) {
        ["@attributes"]=>
        array(4) {
          ["type"]=>
          string(4) "file"
          ["user_type"]=>
          string(6) "Stills"
          ["description"]=>
          string(20) "Sample description 2"
          ["file"]=>
          string(12) "sample-2.jpg"
        }
        ["@value"]=>
        string(0) ""
      }
      [2]=>
      array(2) {
        ["@attributes"]=>
        array(4) {
          ["type"]=>
          string(4) "file"
          ["user_type"]=>
          string(6) "Stills"
          ["description"]=>
          string(20) "Sample description 3"
          ["file"]=>
          string(12) "sample-3.jpg"
        }
        ["@value"]=>
        string(0) ""
      }
      [3]=>
      array(2) {
        ["@attributes"]=>
        array(4) {
          ["type"]=>
          string(4) "file"
          ["user_type"]=>
          string(6) "Stills"
          ["description"]=>
          string(20) "Sample description 4"
          ["file"]=>
          string(12) "sample-4.jpg"
        }
        ["@value"]=>
        string(0) ""
      }
      [4]=>
      array(2) {
        ["@attributes"]=>
        array(4) {
          ["type"]=>
          string(4) "file"
          ["user_type"]=>
          string(6) "Stills"
          ["description"]=>
          string(20) "Sample description 5"
          ["file"]=>
          string(12) "sample-5.jpg"
        }
        ["@value"]=>
        string(0) ""
      }
    }
  }
}

I don’t quite understand why you create an array here, use value instead, which will then already give you an array:

<?php $materials = $item->materials()->value() ?>

Then, it doesn’t make sense to have two loops here, because the first item you want to get is not an iterable array but a single item:

<?php foreach ($page->children()->sortBy('date', 'desc') as $item) : ?>
    <?php $materials = $item->materials()->value(); ?>
    <?= $materials['@attributes']['label'] ?? '' ?>
    <?php foreach ($materials['material'] ?? [] as $material_item) : ?>
        <?= $material_item['@attributes']['description'] ?? '' ?>
    <?php endforeach ?>
<?php endforeach ?>

And for the $material_item you missed the @attributes key, you cannot directly access description here, because the items looks like this:

Array
(
    [@attributes] => Array
        (
            [type] => file
            [user_type] => Stills
            [description] => Sample description 1
            [file] => sample-1.jpg
        )

    [@value] => 
)

Always check what you have with dump() at each step. Getting more familiar with arrays in PHP will help.

Fantastic! Thank you very much for your advice!