KAYA - PHP Online

Form of PHP Sandbox

Enter Your PHP code here for testing/debugging in the Online PHP Sandbox. As in the usual PHP files, you can also add HTML, but do not forget to add the tag <?php in the places where the PHP script should be executed.



Your result can be seen below.

Result of php executing





Full code of KAYA.php

  1. <?php
  2. $a_all_f = array(
  3.         'US' => 0,
  4.         'EU' => 0,
  5.         'TAO' => 0
  6. );
  7.  
  8. $a_common_f = $a_all_f;
  9.  
  10. $a_order = array(
  11.         0 => array(
  12.                 'p-sku'          => 'ABC123',
  13.                 'p-cat'          => 'MANETTE',
  14.                 'p-mapping' => array(
  15.                         'US' => 1,
  16.                         'EU' => 2
  17.                 )
  18.         ),
  19.         1 => array(
  20.                 'p-sku'          => 'DEF456',
  21.                 'p-cat'          => 'MANETTE',
  22.                 'p-mapping' => array(
  23.                         'TAO' => 1
  24.                 )
  25.         ),
  26.         2 => array(
  27.                 'p-sku'          => 'OOO111',
  28.                 'p-cat'          => 'ACCESSORY',
  29.                 'p-mapping' => array(
  30.                         'US' => 3,
  31.                         'TAO' => 1
  32.                 )
  33.         ),
  34.         3 => array(
  35.                 'p-sku'          => 'PPP222',
  36.                 'p-cat'          => 'ACCESSORY',
  37.                 'p-mapping' => array(
  38.                         'US' => 3,
  39.                         'TAO' => 1
  40.                 )
  41.         )
  42. );
  43.  
  44. foreach ($a_order as $a_product) {
  45.     //print_r($a_product);
  46.         $a_tmp = array();
  47.         foreach (array_intersect(array_keys($a_common_f), array_keys($a_product['p-mapping'])) as $key) {
  48.             //print_r($key);
  49.                 $a_tmp[$key] = $a_common_f[$key] + $a_product['p-mapping'][$key];
  50.         }
  51.         //print_r($a_tmp);
  52.         $a_common_f = $a_tmp;
  53. }
  54.  
  55. //print_r($a_common_f);
  56. if (count($a_common_f) > 0) {
  57.         // first KEY of the value-sorted array
  58.         asort($a_common_f);
  59.         $THE_F_TO_USE[array_key_first($a_common_f)] = "tous les sku";
  60. }
  61. else {
  62.         // TODO il faut trier l'array pour avoir MANETTE first puis ACCESSORY
  63.         // ou sinon, il faut faire 2 fois le foreach (laid)
  64.         foreach ($a_order as $a_product) {
  65.                 $a_tmp = $a_product['p-mapping'];
  66.                 asort($a_tmp);
  67.                 if ($a_product['p-cat'] == 'MANETTE') {
  68.                         $THE_F_TO_USE[array_key_first($a_tmp)][] = $a_product['p-sku'];
  69.                 }
  70.                 else if ($a_product['p-cat'] == 'ACCESSORY') {
  71.                         $has_a_f_in_common = 0;
  72.                         foreach (array_keys($a_tmp) as $k_f) {
  73.                                 if (array_key_exists($k_f, $THE_F_TO_USE)) {
  74.                                         $THE_F_TO_USE[$k_f][] = $a_product['p-sku'];
  75.                                         $has_a_f_in_common = 1;
  76.                                         break;
  77.                                 }
  78.                         }
  79.                         // si pas de f en commun, alors comportement par défaut : f la plus prio de l'accessoire
  80.                         if ($has_a_f_in_common == 0) {
  81.                                 $THE_F_TO_USE[array_key_first($a_tmp)][] = $a_product['p-sku'];
  82.                         }
  83.                 }
  84.         }      
  85. }
  86. print_r($THE_F_TO_USE);
  87.  
  88.  
File Description
  • KAYA
  • PHP Code
  • 05 May-2022
  • 1.89 Kb
You can Share it: