从WooCommerce API产品/类别创建层次结构的类别。

3

我正在尝试创建一种数据结构,使我的应用程序可以使用它来将系统中的类别映射到WooCommerce结构。我只需要一个类别层次结构(如下所示),其中包含具有类别子节点ID的每个父/子级别之间的管道的类别完整路径。

我可以使用以下代码轻松获取WooCommerce中的所有产品类别:

    $woocommerce    =   new Client($this->ecommerce_url,$this->ecommerce_api_key,$this->ecommerce_api_cred,array('wp_api' => true,'version' => 'wc/v1'));

    $send_call = true;
    $per_page = 30;
    $categories = array();
    $page = 1;

    $categories = array();

    while($send_call == true) 
    {

        $result_categories = $woocommerce->get('products/categories', array('per_page'=>$per_page, 'page'=>$page,'context' => 'view'));
        $page++;
        $categories = array_merge( $categories, $result_categories );

        if( count($result_categories) < $per_page )
        {
            $send_call=false;
        }

    }

    $return_woo_categories = array();

    foreach($categories as $index => $category)
    {
        $return_woo_categories[] = array('name' => $category['name'], 'id' => $category['id'], 'parent' => $category['parent']);
    }

$return_woo_categories的var_dump结果

array(13) {
  [0]=>
  array(3) {
    ["name"]=>
    string(1) "/"
    ["id"]=>
    int(23)
    ["parent"]=>
    int(20)
  }
  [1]=>
  array(3) {
    ["name"]=>
    string(1) "+"
    ["id"]=>
    int(21)
    ["parent"]=>
    int(20)
  }
  [2]=>
  array(3) {
    ["name"]=>
    string(6) "Albums"
    ["id"]=>
    int(20)
    ["parent"]=>
    int(16)
  }
  [3]=>
  array(3) {
    ["name"]=>
    string(18) "Castle On The Hill"
    ["id"]=>
    int(24)
    ["parent"]=>
    int(23)
  }
  [4]=>
  array(3) {
    ["name"]=>
    string(8) "Clothing"
    ["id"]=>
    int(14)
    ["parent"]=>
    int(0)
  }
  [5]=>
  array(3) {
    ["name"]=>
    string(7) "Hoodies"
    ["id"]=>
    int(15)
    ["parent"]=>
    int(14)
  }
  [6]=>
  array(3) {
    ["name"]=>
    string(5) "Music"
    ["id"]=>
    int(16)
    ["parent"]=>
    int(0)
  }
  [7]=>
  array(3) {
    ["name"]=>
    string(10) "My New Cat"
    ["id"]=>
    int(6)
    ["parent"]=>
    int(0)
  }
  [8]=>
  array(3) {
    ["name"]=>
    string(7) "Posters"
    ["id"]=>
    int(17)
    ["parent"]=>
    int(0)
  }
  [9]=>
  array(3) {
    ["name"]=>
    string(12) "Shape of You"
    ["id"]=>
    int(25)
    ["parent"]=>
    int(23)
  }
  [10]=>
  array(3) {
    ["name"]=>
    string(7) "Singles"
    ["id"]=>
    int(18)
    ["parent"]=>
    int(16)
  }
  [11]=>
  array(3) {
    ["name"]=>
    string(8) "T-shirts"
    ["id"]=>
    int(19)
    ["parent"]=>
    int(14)
  }
  [12]=>
  array(3) {
    ["name"]=>
    string(1) "x"
    ["id"]=>
    int(22)
    ["parent"]=>
    int(20)
  }

我需要一个数据结构作为输出,其中键的顺序并不重要。
$cat_map = array(
    'Clothing' => 14,
    'Clothing|Hoodies' => 15,
    'Clothing|T-shirts' => 19,
    'My New Cat' => 6,
    'Posters' => 17,
    'Music' => 16,
    'Music|Singles' => 18,
    'Music|Albums' => 20,
    'Music|Albums|x' => 22,
    'Music|Albums|/' => 23,
    'Music|Albums|+' => 21,
    'Music|Albums|/|Castle On The Hill' => 24,
    'Music|Albums|/|Shape Of You' => 25,
);

我陷入了困境,不知该如何做;我想我需要使用递归来解决问题;但是让我卡住的是,我从WooCommerce返回的数组顺序没有任何层次结构。(按添加顺序排列,最近的排在最前面)

2个回答

1
最简单的方法是先构建树形(嵌套)结构,然后使用递归函数连接字符串:
$tree = array();
foreach ($return_woo_categories as $cat) {
    if (!isset($tree[$cat['id']])) { $tree[$cat['id']] = array(); }
    $tree[$cat['id']]['name'] = $cat['name'];
    if (!isset($tree[$cat['parent']])) { $tree[$cat['parent']] = array(); }
    $tree[$cat['parent']]['children'][$cat['id']] =& $tree[$cat['id']];
}

function buildPaths($tree, $path = '') {
    $result = array();
    foreach ($tree as $id => $cat) {
        $result[$id] = $path . $cat['name'];
        if (isset($cat['children'])) {
            $result += buildPaths($cat['children'], $result[$id] . '|');
        }
    }
    return $result;
}

$cat_map = buildPaths($tree[0]['children']);

这个 $cat_map 变量的结构是 [id] => 'path',所以需要翻转它以匹配您需要的结果。使用 id 访问路径比反过来更方便,但如果您确实需要这种结构,则最后一行应该是:
$cat_map = array_flip(buildPaths($tree[0]['children']));

0

我会做一个三级循环以便检索

  1. 获取父类别
  2. 获取子类别
  3. 获取子子类别

在每个循环中,我都会连接"|",然后将键=>值添加到每个级别。

以下代码提供了预期的数组

////////////////////////////////////////////////////
 //creating new array to store new structure
 $cat_map = [] ;
//3 level loop to get desired info
foreach($return_woo_categories as $inf){
 //1 loop to get parent categories
    if($inf['parent'] == 0){
             $parentcategoryname = $inf['name'];           
            $cat_map[$parentcategoryname] = $inf['id'];          
            //2 loop to get 1 child category
                foreach($return_woo_categories as $inf2){
                    if($inf['id'] == $inf2['parent'] ){ 
                    //concat first level and second level name               
                    $firstchildname = $inf['name']."|".$inf2['name'];
                        $cat_map[$firstchildname] = $inf2['id'];                 

                    //3 loop to get 2 child category
                     foreach($return_woo_categories as $inf3){

                        if($inf2['id'] == $inf3['parent'] ){

                         $secondchildname = $inf['name']."|".$inf2['name']."|".$inf3['name'];   
                          $cat_map[$secondchildname] = $inf3['id'];    

                        }
                     }
                    }               
                }        
        }
}
//Result
var_dump( $cat_map);
array (size=11)
  'Clothing' => int 14
  'Clothing|Hoodies' => int 15
  'Clothing|T-shirts' => int 19
  'Music' => int 16
  'Music|Albums' => int 20
  'Music|Albums|/' => int 23
  'Music|Albums|+' => int 21
  'Music|Albums|x' => int 22
  'Music|Singles' => int 18
  'My New Cat' => int 6
  'Posters' => int 17

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接