PHP loop assign uuid features to array

I try to build a api with a good performance. This api should import product data via GuzzleHTTP into a shopsystem.

I loop through the xml tags and map the features, if they’re assigned, with the corresponding uuids of the shopsystem.

$articleData['properties'][X]['id'] = property uuid of shopsystem

It works fine. But it’s builded very static and feels faulty. Now I am trying to improve the performance.

The xml can contain up to thousends of articles with up to 300 of different features. Each of those features is a property option in the shopsystem.

Is there a better way to map the features to the corresponding uuid’s instead of a static if elseif – loop? I were thinking about a mapping table with every feature and It’s uuid… But I have no clue how to start. Please help.

XML file:

<exportxml>
  <article key="1">
    <datagroup1>
      <feature key="feature1">
        <eng> XYZ </eng>
        <ger> XYZ </ger>
        <esp> XYZ </esp>
      </feature>
      <feature key="feature2">
        <eng> XYZ </eng>
        <ger> XYZ </ger>
        <esp> XYZ </esp>
      </feature>
      [...]
    </datagroup1>
  </article>
  <article key="2">
    <datagroup1>
      <feature key="feature1">
        <eng> ZYX </eng>
        <ger> ZYX </ger>
        <esp> ZYX </esp>
      </feature>
      [...]
    </datagroup1>
  </article>
</exportxml>

PHP loop:

     foreach ($XML->article as $article) {
        foreach ($article->datagroup1->feature as $feature) {
           if ($feature['key'] == "feature1") {
                $feature1 = (string)$feature->en;
                if ($featue1 == 'XYZ') {
                    $articleData['properties'][1]['id'] = 'dfa2394bcf754a19b9e72d14207ad7d0';
                } elseif ($feature1 == 'XZY') {
                    $articleData['properties'][1]['id'] = '582a5b9654564055a2e56a0f9a7a399e';
                } elseif ($feature1 == 'YZX') {
                    $articleData['properties'][1]['id'] = '582a5b9654564055a2e56a0f9a7a399e';
                }
           elseif ($feature['key'] == "feature2") {
                if ($featue2 == 'XYZ') {
                    $articleData['properties'][2]['id'] = 'ert2394bcf754a19b9e72d14207ad7d0';
                } elseif ($feature2 == 'XZY') {
                    $articleData['properties'][2]['id'] = 'op2a5b9654564055a2e56a0f9a7a399e';
                } elseif ($feature2 == 'YZX') {
                    $articleData['properties'][2]['id'] = '1sd5b9654564055a2e56a0f9a7a399e';
                }
           }
           [...]
        }
     }

Answer

You can create an array of $features with uuid and then you can improve your code. example :

<?php

$features = [
    'feature1' =>[
        'XYZ' => 'dfa2394bcf754a19b9e72d14207ad7d0',
        'XZY' => '582a5b9654564055a2e56a0f9a7a399e',
        'YZX' => '582a5b9654564055a2e56a0f9a7a399e'
    
    ],
    'feature2' =>[
        'XYZ' => 'ert2394bcf754a19b9e72d14207ad7d0',
        'XZY' => 'op2a5b9654564055a2e56a0f9a7a399e',
        'YZX' => '1sd5b9654564055a2e56a0f9a7a399e'
    ]
];


foreach ($XML->article as $article) {
    foreach ($article->datagroup1->feature as $key => $feature) { // introduce $key
        if(isset($features[$feature['key']])){
            $featureName = (string)$feature->en;
            if(isset($features[$feature['key']][$featureName])){
                $articleData['properties'][$key]['id'] = $features[$feature['key']][$featureName]; // use $key
            }
        }
    }
 }

https://3v4l.org/pjqk8

Note:

Above is hypothetical, as I am not sure you can do that or not?

Make sure you are checking every conditions at your end, as by looking your code data, it’s not fully clear about conditions you want to check.