Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add 'nest' method to ArrayHelper #6

Open
johonunu opened this issue Mar 23, 2017 · 6 comments
Open

Add 'nest' method to ArrayHelper #6

johonunu opened this issue Mar 23, 2017 · 6 comments
Labels
type:feature New feature

Comments

@johonunu
Copy link

I think we would be nice to introduce method 'nest' similar to CakePHP one to ArrayHelper. It is used to create threaded data (with children nodes).

More information regarding it:
https://book.cakephp.org/3.0/en/core-libraries/hash.html#Cake\Utility\Hash::nest
https://github.com/cakephp/utility/blob/master/Hash.php#L1172

Is it possible to get same response using ArrayHelper::map somehow ?

@yii-bot
Copy link

yii-bot commented Mar 23, 2017

Thank you for putting effort in the improvement of the Yii framework.
We have reviewed your pull request.

Unfortunately a use case is missing. It is required to get a better understanding of the pull request and helps us to determine the necessity and applicability of the suggested change to the framework.

Could you supply us with a use case please? Please be as detailed as possible and show some code!

Thanks!

This is an automated comment, triggered by adding the label pr:missing usecase.

@johonunu
Copy link
Author

@samdark

Here are some use cases:

@samdark
Copy link
Member

samdark commented Mar 24, 2017

  • When you want to generate/print multi-level structure for comments
  • When you want to generate/print multi-level menu

Why aren't you iterating and outputting it right away?

protected function renderComments($parentID = null, $i = 0)
{
    $out = '';
    foreach ($this->comments as $comment) {
        if ($comment->parent_id === $parentID) {
            
            $out .= $this->render('comment', [
                'comment' => $comment, 'class' => $parentID !== null && $i !== 0 ? 'child-comment' : ''
            ]);
            $i++;
            $out .= $this->renderComments($comment->id, $i);
        }
    }
    
    return $out;
}
  • When you want to generate tree (nested set) for use with js plugins

OK, that one makes sense...

@samdark
Copy link
Member

samdark commented Mar 24, 2017

Is it possible to get same response using ArrayHelper::map somehow ?

No.

@SamMousa
Copy link

@johonunu CakePHP releases their utility classes in a separate package; you could use that.

https://packagist.org/packages/cakephp/utility

@johonunu
Copy link
Author

@SamMousa I know that, I just wanted to note that it would be good idea to have it in the core. Most of the methods in Hash class are available inside ArrayHelper, but something like "nest" isn't. I can live without it in core, if I am the only one missing it ;)

@samdark samdark transferred this issue from yiisoft/yii2 Apr 18, 2019
@samdark samdark added the type:feature New feature label Sep 11, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type:feature New feature
Projects
None yet
Development

No branches or pull requests

4 participants