Closed lablnet closed 5 years ago
Now last method
is left still
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.
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
- 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 .
@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
Expexted output you already knows that discussed aboved help to resolve this case.
$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.
@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
I think we should implement four more methods in our
Arrays
class1. Remove duplicate
there should be method namely
removeDuplicates()
which remove duplicate records in array consider following data setLook 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
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
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
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