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

Why does \filter sometime return an array of arrays? #39

Closed
gabrielflorit opened this issue Jul 3, 2014 · 1 comment
Closed

Why does \filter sometime return an array of arrays? #39

gabrielflorit opened this issue Jul 3, 2014 · 1 comment

Comments

@gabrielflorit
Copy link

Say we have a $towns array:

[
  {
    town: "Easton",
    customers: 5
  }, {
    town: "Andover",
    customers: 9
  }
]

The following

$result = F\filter($towns, function($town) {
  return $town->customers > 5;
});

returns an array of items keyed by the original position:

{
  1: {
    town: "Andover",
    customers: 9
  }
}

but comparing greater than -1

$result = F\filter($towns, function($town) {
  return $town->customers > -1;
});

returns the expected structure

[
  {
    town: "Easton",
    customers: 5
  }, {
    town: "Andover",
    customers: 9
  }
]

Any thoughts?

@lstrojny
Copy link
Owner

lstrojny commented Jul 4, 2014

filter() and Functional PHP in general tries to preserve keys whereever possible. As I am guessing you are doing json_encode() on the data, do array_values(json_encode()) if you want to force a list as a result. json_encode() encodes an array as an object if there is no index 0, array_values() can fix that for you.

@lstrojny lstrojny closed this as completed Oct 8, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants