Skip to content

[PropertyAccess] array_keys does not work for ArrayAccess objects #10705

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

Closed
wants to merge 5 commits into from

Conversation

sandermarechal
Copy link
Contributor

Q A
Bug fix? yes
New feature? no
BC breaks? no
Deprecations? no
Tests pass? yes
Fixed tickets
License MIT
Doc PR

The array_keys() function does not work on objects implementing \ArrayAccess. Skip listing the known properties for such objects.

@stof
Copy link
Member

stof commented Apr 14, 2014

please add a test covering this

and please add the PR summary table in the description

@romainneutron
Copy link
Contributor

If $objectOrArray is Traversable, keys could be extracted.

@sandermarechal
Copy link
Contributor Author

@stof Will do when I get back to work tomorrow.

@fabpot
Copy link
Member

fabpot commented Apr 28, 2014

@sandermarechal any news on this PR?

@sandermarechal sandermarechal changed the title array_keys does not work for ArrayAccess objects [PropertyAccess] array_keys does not work for ArrayAccess objects Apr 30, 2014
@sandermarechal
Copy link
Contributor Author

@fabpot @stof Done. Sorry it took a while longer.

@fabpot
Copy link
Member

fabpot commented Apr 30, 2014

Can you also take care of the case where the var is Traversable?

@sandermarechal
Copy link
Contributor Author

Done. Note: I don't know why fabbot's Travis build fails. The error if gives doesn't match my source code. Perhaps this is caused by the same reason that I cannot rebase this on master without causing a conflict?

if (is_array($objectOrArray)) {
$keys = '"' . print_r(array_keys($objectOrArray), true) . '"';
} elseif ($objectOrArray instanceof \Traversable) {
$list = [];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should be array() to keep BC with PHP 5.3.

foreach ($objectOrArray as $key => &$value) {
$list[] = $key;
}
$keys = '"'.print_r($list, true).'"';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd use implode() with ", "

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The original code also used print_r() so I left it that way.

@webmozart
Copy link
Contributor

Hi @sandermarechal, thank you for your work on this issue! I fixed this, together with a few other improvements, in #10946 and #10947.

@webmozart webmozart closed this May 20, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants