-
-
Notifications
You must be signed in to change notification settings - Fork 202
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
Function to build associative arrays from an input list and two user functions producing keys and values for the output list #164
Comments
|
@phanan Sorry, I messed up explaining what I want. Basically what I suggest is an analogue to Yii2's
and creates an associative array (a map) from the keys to values for the same elements of the original array, e.g.: ArrayMap::map(
[
['id' => 1, 'name' => 'Bob'],
['id' => 2, 'name' => 'Joj'],
['id' => 3, 'name' => 'Yoy'],
],
function($elem, $index) {
return $elem['id'];
},
function($elem, $index) {
return 'Mister '.$elem['name'];
}
);
/*
returns
[
1 => 'Mister Bob',
2 => 'Mister Joj',
3 => 'Mister Yoy'
]
*/ Example: I have a list of books. I need to get a map from book ids to their names to pass to a view to render a In plain PHP: $ids2Names = [];
foreach ($books as $book) {
$ids2Names[$book->id] = $book->name;
} With ArrayHelper::map(
$books,
function($book) { return $book->id; },
function($book) { return $book->name; }
); With create_assoc(
$books,
function($book) { return $book->id; },
function($book) { return $book->name; }
); Benefit of this approach in comparison to plain php is that it is more semantical and isolates logic for getting keys and values, doesn't require you to come up with the assotiative array name. We can also add a check that no two entries of the input array produce the same key. |
@phanan Also |
|
array_combine(
map($books, function($book) {
return $book->id;
}),
map($books, function($book) {
return $book->name;
})
); vs create_assoc(
$books,
function($book) { return $book->id; },
function($book) { return $book->name; }
); Also if we want to do something like this: create_assoc(
filter(
$bookRepository->getBooks(),
function() {
// maybe some more logic
}
),
function($book) { return $book->id; },
function($book) { return $book->name; }
); then in the Also |
Personally, I don't see a big gain :/ Btw, your create_assoc(
$books,
function($book) {
return $book->id;
},
function($book) {
return $book->name;
}
); As you can see, |
@phanan It is not about LoC, it is about the amount of nested expressions ;;;))) Btw, if you rewrite my array_combine(
map(
$books,
function($book) {
return $book->id;
}
),
map(
$books,
function($book) {
return $book->name;
}
)
); |
Ok, let's wait several months till the few people who haven't already searched for a less verbose way to build associative arrays in PHP start upvoting this coming via Google, if you don't think this suggestion is useful. |
Another option that came to mind was to use reindex:
But I like that the |
What do you think about unfold? It does already what you are looking for, if the passed function is unary is called with just the input as argument, and allows optional values or even a helper to pass around values between applied functions. We use it to apply a bunch of different service calls to a single entry, like a tap but calling many functions to build a single assoc array. Happy to merge this little function of mine with more tests and a better suited name to the project. |
So @gvlasov, my proposal it very similar to what you have requested, but instead of directly building the associative array, instead I'm returning a function that allow to build any number of associative arrays, based on the same set of rules with a single or a series of input values. If there are some issues on my proposal, please let me know to make it more useful. My tests are copied from the independent project I've worked this function, and they are thought more closely to a complement of another library that I use to map associative arrays, reducing them being a common case. This function makes the opposite, that's why in the little collection I've built I called it originally |
Hi, Is this what you're looking for? |
This is a feature request. Something like this:I'm not sure what that function would be called, I admit that it should have a better name thancreate_map
Couldn't properly explain what I want in the original post, see a comment below: #164 (comment)
The text was updated successfully, but these errors were encountered: