PHP-通过他们的孩子的链阵列项目

发布于 2025-01-24 10:56:10 字数 892 浏览 0 评论 0原文

我在PHP中有以下各种项目:

0 => [
        'name' => 'retail',
        'id' => 1,
        'following' => null
],
1 => [
        'name' => 'industry',
        'id' => 2,
        'following' => 3
],
2 => [
        'name' => 'entertainment',
        'id' => 3,
        'following' => 1
],
3 => [
        'name' => 'sports',
        'id' => 4,
        'following' => null
],
4 => [
        'name' => 'construction',
        'id' => 5,
        'following' => null
],
5 => [
        'name' => 'music',
        'id' => 6,
        'following' => 5
]
...

我想链接每个项目的名称,并从父母到孩子的订购。上面的示例将给出结果:

[
        0 => 'industry / entertainment / retail',
        1 => 'sports',
        2 => 'music / construction'
]

每个项目都可以拥有尽可能多的孩子,并且订单或项目是随机的。从几天开始,我正在研究这个问题,但我仍然没有成功。有帮助吗?

I have the following array of items in PHP :

0 => [
        'name' => 'retail',
        'id' => 1,
        'following' => null
],
1 => [
        'name' => 'industry',
        'id' => 2,
        'following' => 3
],
2 => [
        'name' => 'entertainment',
        'id' => 3,
        'following' => 1
],
3 => [
        'name' => 'sports',
        'id' => 4,
        'following' => null
],
4 => [
        'name' => 'construction',
        'id' => 5,
        'following' => null
],
5 => [
        'name' => 'music',
        'id' => 6,
        'following' => 5
]
...

and I would like to chain the names of each items and ordering them from parent to child. The example above would give the result :

[
        0 => 'industry / entertainment / retail',
        1 => 'sports',
        2 => 'music / construction'
]

Each item could have as many as child as possible, and the order or the items is random. I am working on this since a few days and I have still no success. Any help please ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

自控 2025-01-31 10:56:10
  1. 创建/格式pollowings作为id的数组ID:[plost_id]
$items = [
    0 => [
        'name' => 'retail',
        'id' => 1,
        'following' => null
    ],
    1 => [
        'name' => 'industry',
        'id' => 2,
        'following' => 3
    ],
    2 => [
        'name' => 'entertainment',
        'id' => 3,
        'following' => 1
    ],
    3 => [
        'name' => 'sports',
        'id' => 4,
        'following' => null
    ],
    4 => [
        'name' => 'construction',
        'id' => 5,
        'following' => null
    ],
    5 => [
        'name' => 'music',
        'id' => 6,
        'following' => 5
    ]
];

// STEP 1
$followings = [];

foreach($items as $id => $data){
    $item_id = $items[$id]['id'];
    if(isset($items[$id]['following']))
        $followings[$item_id] = [$items[$id]['following']];
    else
        $followings[$item_id] = [];
}

// STEP 2
function chain(&$arr, $i){

    if(empty($arr[$i])){
        return;
    }

    $last_follow_id = end($arr[$i]);

    if(empty($arr[$last_follow_id])){
        unset($arr[$last_follow_id]);
        return;
    }

    chain($arr, $last_follow_id);
    $arr[$i] = array_merge($arr[$i], $arr[$last_follow_id]);
    unset($arr[$last_follow_id]);
}


foreach ($followings as $id => $follows) {
    chain($followings, $id);
}

// STEP 3
foreach($followings as $first => $remaining){
    if(empty($remaining)){
        $followings[$first] = $items[$first - 1]['name'];
        continue;
    }

    $chain = $items[$first - 1]['name'] . ' / ';
    foreach($remaining as $r){
        $chain .= $items[$r - 1]['name'] . ' / ';
    }
    $chain = substr($chain, 0, -3);
    $followings[$first] = $chain;
}

// Reindex if needed
print_r(array_values($followings));
  1. Create/Format followings as an array of id: [following_id]
  2. Chain followings recursively
  3. Create strings from the follow chains
$items = [
    0 => [
        'name' => 'retail',
        'id' => 1,
        'following' => null
    ],
    1 => [
        'name' => 'industry',
        'id' => 2,
        'following' => 3
    ],
    2 => [
        'name' => 'entertainment',
        'id' => 3,
        'following' => 1
    ],
    3 => [
        'name' => 'sports',
        'id' => 4,
        'following' => null
    ],
    4 => [
        'name' => 'construction',
        'id' => 5,
        'following' => null
    ],
    5 => [
        'name' => 'music',
        'id' => 6,
        'following' => 5
    ]
];

// STEP 1
$followings = [];

foreach($items as $id => $data){
    $item_id = $items[$id]['id'];
    if(isset($items[$id]['following']))
        $followings[$item_id] = [$items[$id]['following']];
    else
        $followings[$item_id] = [];
}

// STEP 2
function chain(&$arr, $i){

    if(empty($arr[$i])){
        return;
    }

    $last_follow_id = end($arr[$i]);

    if(empty($arr[$last_follow_id])){
        unset($arr[$last_follow_id]);
        return;
    }

    chain($arr, $last_follow_id);
    $arr[$i] = array_merge($arr[$i], $arr[$last_follow_id]);
    unset($arr[$last_follow_id]);
}


foreach ($followings as $id => $follows) {
    chain($followings, $id);
}

// STEP 3
foreach($followings as $first => $remaining){
    if(empty($remaining)){
        $followings[$first] = $items[$first - 1]['name'];
        continue;
    }

    $chain = $items[$first - 1]['name'] . ' / ';
    foreach($remaining as $r){
        $chain .= $items[$r - 1]['name'] . ' / ';
    }
    $chain = substr($chain, 0, -3);
    $followings[$first] = $chain;
}

// Reindex if needed
print_r(array_values($followings));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文