PHP 循环分配 uuid 特征到数组

PHP loop assign uuid features to array

我尝试构建一个性能良好的 api。 api 应通过 GuzzleHTTP 将产品数据导入商店系统。

我遍历 xml 标签并映射特征(如果已分配)与商店系统的相应 uuid。

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

它工作正常。但它的构建非常静态,感觉有问题。现在我正在努力提高性能。

xml 可以包含多达 300 种不同功能的数千篇文章。这些功能中的每一个都是商店系统中的一个 属性 选项。

是否有更好的方法将特征映射到相应的 uuid 而不是静态 if elseif - 循环?我正在考虑每个功能的映射 table 并且它是 uuid ...但我不知道如何开始。请帮忙。

XML 文件:

<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循环:

     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';
                }
           }
           [...]
        }
     }

您可以创建一个 $featuresuuid 的数组,然后您可以改进您的代码。示例:

<?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

注:

以上是假设,因为我不确定你能不能做到?

确保你在最后检查了每一个条件,因为通过查看你的代码数据,它并不完全清楚你想要检查的条件。