How to add to associative array value of the attributes() of element xml?

Good day, may the wrong question, do not judge strictly. The essence of the problem is this, there is a standard xml document
<offer id="129" available="true" >
<url>http://api.loc/index.php?route=product/product&amp;path=3899&amp;product_id=131</url>
<price>850</price>
<currencyId>UAH</currencyId>
<categoryId>3899</categoryId>
<delivery>true</delivery>
<stock_quantity>100</stock_quantity>
<name>Sandals 82BLUE pp. 26 17 cm Blue</name>
<vendor>(manufacturer not specified)</vendor>
<barcode>200</barcode>
<param name="Size">26</param>
<param name="Size insoles">12</param>
</offer>
<offer id="130" available="true" >
<url>http://api.loc/index.php?route=product/product&amp;path=3899&amp;product_id=130</url>
<price>850</price>
<currencyId>UAH</currencyId>
<categoryId>3899</categoryId>
<delivery>true</delivery>
<stock_quantity>100</stock_quantity>
<name>Sandals 82BLUE R. 27 17.5 cm Blue</name>
<vendor>(manufacturer not specified)</vendor>
<barcode>200</barcode>
<param name="Shoe Size">46</param>
<param name="Size insoles">19</param>
</offer>

Want to create an associative array such plan
[0] => Array
(
 [id] => 200
 [size] => 26
 [stelka] => 12
)
 [1] => Array
(
 [id] => 200
 [size] => 46
 [stelka] => 19
 )

Trying to do so
$join = [];
 foreach($xml->shop->offers->offer as $offer)
{
 $id = (string)$offer->barcode;

 $join[$k] = [
 'id' => $id,
 'size' => (string) $offer->param[0],
 'stelka' => (string) $offer->param[1]
];
$k++;
 }

But when printing the array $join, it turns out
[] => Array
(
 [id] => 200
 [size] => 46
 [stelka] => 19
 )

But the problem is that in the array $join is added not all of the available offers from the xml, but only the last one. What could be the problem and what I did wrong?
P. S. Sorry, the question is closed, a trivial error in syntax))
April 3rd 20 at 18:55
1 answer
April 3rd 20 at 18:57
Solution
When you turns the document into a SimpleXMLElement, each DOCHERNEE Noda also it is and it also has its own set atributov $element->attributes()
The final code

<?php

$string = <<<XML
<offer id="130" available="true" >
<url>http://api.loc/index.php?route=product/product&amp;path=3899&amp;product_id=130</url>
<price>850</price>
<currencyId>UAH</currencyId>
<categoryId>3899</categoryId>
<delivery>true</delivery>
<stock_quantity>100</stock_quantity>
<name>Sandals 82BLUE R. 27 17.5 cm Blue</name>
<vendor>(manufacturer not specified)</vendor>
<barcode>200</barcode>
<param name="Shoe Size">46</param>
<param name="Size insoles">19</param>
</offer>
XML
;

$xml = new SimpleXMLElement($string);

$params = [];
// All nodes with the tag param left the array, each of which SimpleXMLElement
foreach($xml->param as $param) {
 // and also @attributes, where all attributes of the current element, get the one name
 $paramName = $param->attributes()['name'];
 $params[(string) $paramName] = (string) $param;
}

var_dump($params);
// [
// "Shoe size" => string(2) "46"
// "The size of the insole" => string(2) "19
// ]


UPD: To all hands not to write and not to guess which option came how the account (and can come in a single unloading in a different order due to the fact that one product is simply not listed one of the options I suggest to do so:

1. Make a chart of all matches
$featuresMap = [
 'Shoe size' => 'size',
 'Size insoles' => 'stelka',
];

2. And already dynamically collect each element in the map settings
foreach($xml->param as $param) {
 $paramName = $param->attributes()['name'];
 $paramAlias = $featuresMap[(string) $paramName]
 $params[$paramAlias] = (string) $param;
}


And then you will only need to expand the map matching, and the loop code will always remain the same. Well, adding a couple of checks and for example, to transliterate the new values by adding them to the database, and the map is taken from the same database.
Thank You so much, really did not consider that the attribute can otstavat. Your solution would indeed be correct - vernie.Halvors commented on April 3rd 20 at 19:00

Find more questions by tags PHP