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

Remove without field #39

Open
chrisvanpatten opened this issue May 13, 2020 · 2 comments
Open

Remove without field #39

chrisvanpatten opened this issue May 13, 2020 · 2 comments

Comments

@chrisvanpatten
Copy link

I did a bit of experimentation with a PR but wasn't quite able to get it to work, so I thought I'd take this to an issue to look for some ideas on how to implement :)

Given the sample document, I'm interested in being able to use remove() without providing a field, e.g. removing all matching paths.

Essentially supporting something like this:

$jsonObject = new JsonObject($json);

$jsonObject->remove("$..book[?(@.category == 'fiction')]");

$results = $jsonObject->toValue();

// $results with all fiction books removed.

I tried a simple unset($element); in the remove code but PHP's unset only unsets the reference, not the original value, so it doesn't quite work.

@Galbar
Copy link
Owner

Galbar commented May 29, 2020

Hi there! Sounds like a good addition.

Have you tried passing an optional parameter to getReal() stating the action, ie.remove, and have the remove logic happen when traversing the internal array? Instead of appending the match to $selection you could unset it from its parent.

@fbruno93
Copy link

Hi !
Here some workarround

$exploded = explode(".",  $fieldName);
$field = array_pop($exploded);
$imploded = implode(".", $exploded);
$jsonPath = "$";
if (!empty($imploded)) {
    $jsonPath .= ".".$imploded;
}
$jsonPathObject->remove($jsonPath, $field);

It's works on simple cases like $.store.bicycle.price

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

3 participants