带有大于符号的 PHP 提取数组


PHP Extract Array with greater than sign?

>我的数组看起来像

Array
(
    [0] => Array
        (
            [id] => 1
            [parent_id] => 0
            [title] => Mobiles & Tablets
            [childs] => Array
                (
                    [0] => Array
                        (
                            [id] => 10
                            [parent_id] => 1
                            [title] => Mobile Phone
                        )
                )
        )
    [1] => Array
        (
            [id] => 5
            [parent_id] => 0
            [title] => Mobile Phones
            [childs] => Array
                (
                    [0] => Array
                        (
                            [id] => 2
                            [parent_id] => 5
                            [title] => Moto G
                            [childs] => Array
                                (
                                    [0] => Array
                                        (
                                            [id] => 3
                                            [parent_id] => 2
                                            [title] => Android
                                        )
                                )
                        )
                    [1] => Array
                        (
                            [id] => 4
                            [parent_id] => 5
                            [title] => Iphone
                        )
                )
        )
    [2] => Array
        (
            [id] => 6
            [parent_id] => 0
            [title] => test
        )
    [3] => Array
        (
            [id] => 7
            [parent_id] => 0
            [title] => Men's Fashion
            [childs] => Array
                (
                    [0] => Array
                        (
                            [id] => 12
                            [parent_id] => 7
                            [title] => Clothing
                            [childs] => Array
                                (
                                    [0] => Array
                                        (
                                            [id] => 8
                                            [parent_id] => 12
                                            [title] => Jeans
                                        )
                                )
                        )
                )
        )
    [4] => Array
        (
            [id] => 9
            [parent_id] => 0
            [title] => test
        )
)

我为提取数组编写 php 代码。

public $string = '';
public $result = array();
public function getCategoryListsTree( array $items ){
    $this->createCategoryExtractNode($items);
    echo '<pre>';
    print_r($this->result);
    exit;
}
public function createCategoryExtractNode(array $items)
{
    foreach ($items as $key => $value) {
        $this->string .= $value['title'].' > ';
        if(isset($value['childs'])){
            $this->string .= $this->createCategoryExtractNode($value['childs']);
        }else{
            $this->result[$value['id']] = trim($this->string,' > ');
            $this->string = '';
        }
    }
}

输出为

Array
(
    [10] => Mobiles & Tablets > Mobile Phone
    [3] => Mobile Phones > Moto G > Android
    [4] => Iphone
    [6] => test
    [8] => Men's Fashion > Clothing > Jeans
    [9] => test
)

我想要这样的输出

Array
(
    [10] => Mobiles & Tablets > Mobile Phone
    [3] => Mobile Phones > Moto G > Android
    [4] => Mobile Phones > Iphone
    [6] => test
    [8] => Men's Fashion > Clothing > Jeans
    [9] => test
)

请帮忙

谢谢

全局

变量是邪恶的,即使它们被包裹在花哨的OO风格中。 $string是每个createCategoryExtractNode调用的本地上下文。这完成了这项工作:

public $result = array();
public function getCategoryListsTree( array $items ){
    $this->createCategoryExtractNode($items);
    echo '<pre>';
    print_r($this->result);
    exit;
}
public function createCategoryExtractNode(array $items, $carrier='')
{
    foreach ($items as $key => $value) {
        $string = $carrier . $value['title'].' > ';
        if(isset($value['childs'])){
            $this->createCategoryExtractNode($value['childs'], $string);
        }else{
            $this->result[$value['id']] = trim($string,' > ');
        }
    }
}

您需要在每个时刻保持当前path,因此让我们在类定义中添加相应的属性而不是string

public $path = [];

我们将一个元素推送到path数组,最后弹出数组。因此path属性数组保留项目的当前路径。路径项在到达叶节点时用">"粘合:

public function createCategoryExtractNode(array $items)
{
    foreach ($items as $key => $value) {
        array_push($this->path, $value['title']);
        if (isset($value['childs'])) {
            $this->string .= $this->createCategoryExtractNode($value['childs']);
        } else {
            $this->result[$value['id']] = implode(' > ', $this->path);
        }
        array_pop($this->path);
    }
}

这也完成了工作:

public function createCategoryExtractNode(array $items, $breadcrumbs =  array(), $level = 0)
 {
  foreach ($items as $key => $value) {
        $breadcrumbs[$level] = $value['title'].' > ';
    if(isset($value['childs'])){
        $this->createCategoryExtractNode($value['childs'], $breadcrumbs, $level+1);
    }else{
        $this->string = implode('',$breadcrumbs);
        $this->result[$value['id']] = trim($this->string,' > ');
        $this->string = '';
    }
}
}