alphazframework / framework

Core files of AlphaZ Framework
https://alphazframework.github.io/
MIT License
16 stars 17 forks source link

Enhancement of Arrays class #223

Closed lablnet closed 5 years ago

lablnet commented 5 years ago

I think we should implement four more methods in our Arrays class

1. Remove duplicate

there should be method namely removeDuplicates() which remove duplicate records in array consider following data set

        $dataSet = [
            'users' => 
            [
                'id' => 1,
                'name' => "Umer",
                'username' => 'peter',
            ],
            [
                'id' => 2,
                'name' => "Peter Khot",
                'username' => 'peter',
            ]
        ];

Look at above problem set and see the username become duplicate, we don't think how it happen, but there should be way/method which remove duplicate by specific key pair.

2. Pluck

To retrieves all of the values for a given key from an array Consider following dataset

        $array = [
            ['developer' => ['id' => 1, 'name' => 'Umer']],
            ['developer' => ['id' => 2, 'name' => 'Peter']],
        ];

if we do something following Arr:ays:pluck($array, 'developer.name'); it should return both name.

3. Most Occurring

Like anyone wants return most occuring name from array so there should be method that works with every datatype to get most occurring values. consider the following dataset

        $dataSet = [
            'users' => 
            [
                'id' => 1,
                'name' => "Umer",
                'username' => 'peter',
            ],
            [
                'id' => 2,
                'name' => "Umer",
                'username' => 'umer01'
            ],
            [
                'id' => 3,
                'name' => "Peter Khot",
                'username' => 'peter',
            ]
        ];

so if we wants most occuring name it should return Umer [It should works with every data type even with bool]

4 Least Occurring

Like anyone wants return least occuring name from array so there should be method that works with every datatype to get most occurring values. consider the following dataset

        $dataSet = [
            'users' => 
            [
                'id' => 1,
                'name' => "Umer",
                'username' => 'peter',
            ],
            [
                'id' => 2,
                'name' => "Umer",
                'username' => 'umer01'
            ],
            [
                'id' => 3,
                'name' => "Peter Khot",
                'username' => 'peter',
            ]
        ];

so if we wants least occuring name it should return Peter Khot [It should works with every data type even with bool]

Here above all are real world problem sets

What do you think @peter279k and @Maikuolan

lablnet commented 5 years ago

Now last method is left still

2. Pluck

To retrieves all of the values for a given key from an array Consider following dataset

        $array = [
            ['developer' => ['id' => 1, 'name' => 'Umer']],
            ['developer' => ['id' => 2, 'name' => 'Peter']],
        ];

if we do something following Arr:ays:pluck($array, 'developer.name'); it should return both name.

peter279k commented 5 years ago

Please consider about the array_column function.

https://www.php.net/manual/en/function.array-column.php

Muhammad Umer Farooq notifications@github.com 於 2019年5月13日 週一 下午5:12 寫道:

Now last method is left still

  1. Pluck

To retrieves all of the values for a given key from an array Consider following dataset

    $array = [            ['developer' => ['id' => 1, 'name' => 'Umer']],            ['developer' => ['id' => 2, 'name' => 'Peter']],        ];

if we do something following Arr:ays:pluck($array, 'developer.name'); it should return both name.

— You are receiving this because you were assigned. Reply to this email directly, view it on GitHub https://github.com/zestframework/Zest_Framework/issues/223#issuecomment-491742493, or mute the thread https://github.com/notifications/unsubscribe-auth/ACE2SMZCIEJW2GDXVFMW5F3PVEWJVANCNFSM4HJ5X6SA .

lablnet commented 5 years ago

@peter279k i tried following code


    public static function pluck(array $array, $key, $opr = '.')
    {
        if (self::isMulti($array)) {
            $dataSet = [];
            foreach (explode($opr, $key) as $k) {
                print_r($k);
                    $dataSet = array_column($array, $k);
                    //$dataSet = $array[$k];
                    print_r($dataSet);
            }

            return $dataSet;
        }

        throw new \InvalidArgumentException("The array given should be multi-dimensional array", 500);

    }

Usage

        $array = [
            ['developer' => ['id' => 1, 'name' => 'Alex']],
            ['developer' => ['id' => 2, 'name' => 'Peter']],
        ];
        $arrTest = [1,2,3,1,4,6];
        echo "</pre><hr/><h2>Most Occuring</h2><pre>";
        //print_r($dataSet);
        print_r(Arrays::pluck($array, 'developer.id'));
        echo "</pre>";

Output Screenshot_3

Expexted output you already knows that discussed aboved help to resolve this case.

peter279k commented 5 years ago
 $array = [
            ['developer' => ['id' => 1, 'name' => 'Alex']],
            ['developer' => ['id' => 2, 'name' => 'Peter']],
        ];

The array_column is only worked on the following array:

$array = [
    ['id' => 1, 'name' => 'Alex'],
    ['id' => 2, 'name' => 'Peter'],
];

If we want to accept the array you specify, It's not good enough to use array_column.

My approach is as follows:

If we want to use array_column, we have to try generating the column-based array firstly.

lablnet commented 5 years ago

@peter279k Thanks for your reply, i have found another good solution, this is required only last key which we want to search here in above case is id or name let me add that code in PR