简体   繁体   中英

Create an attributes array => values ​without duplicates

I develop a module under prestashop and I retrieve from an API a list of articles with variations. here is the form of an article.

    array:24 [▼
  "is_default" => false
  "options" => []
  "_id" => "5dc40f2f8ec93f00089d2408"
  "name" => "LONGONI noir à scratch - Gant"
  "full_price" => 21
  "reference" => "1301100017"
  "in_stock" => true,
  "variations" => array:1 [▼
    0 => array:3 [▼
      "priority" => 0
      "key" => "main"
      "values" => array:2 [▼
        0 => array:2 [▼
          "priority" => 0
          "value" => "droite"
        ]
        1 => array:2 [▼
          "priority" => 1
          "value" => "gauche"
        ]
      ]
    ]
  ]
  "declinations" => array:2 [▼
    0 => array:10 [▼
      "taxes" => array:1 [▶]
      "id" => "k2oozxne"
      "reference" => "1301100017DL"
      "full_price" => 21
      "in_stock" => true
      "deprecated" => false
      "test" => false
      "ignore_stock" => true
      "variations" => array:1 [▶]
      "stock_quantity" => 0
    ]
    1 => array:10 [▼
      "taxes" => array:1 [▶]
      "id" => "k2oozxnf"
      "reference" => "1301100017GL"
      "full_price" => 21
      "in_stock" => true
      "deprecated" => false
      "test" => false
      "ignore_stock" => true
      "variations" => array:1 [▶]
      "stock_quantity" => -1
    ]
  ]
]

inside each article there are "variations" I would like to be able to create a table including all the variations UNIQUE with the values UNIQUE.

I started doing something like this which gets all the variations but without the values. I would like to be able to inject all the values that correspond to the variations into it. Example:

main => [
'droite',
'gauche']

$attributes = [];
            foreach ($articles as $article) {
                if ($article['variations']) {
                    foreach ($article['variations'] as $key => $variation) {
                        $attrName = trim(str_replace(':', '', $variation['key']));
                        $attributes[] = $attrName;
                    }
                }
            }

            dump(array_unique($attributes));

Thank you for your help.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM