Trim data from JSON that doesn't include defined array?

json trim whitespace
remove indentation from json
how to remove whitespace from json string php
spaces in json string
trim all values in object javascript
remove spaces in json keys
format json remove spaces
trim javascript

There's JSON data with Offers

foreach($json['offers'] as $offer) 

Every offer has data that contains country

"countries":[{"name":"United States","code":"US"}]

How do I remove all the offers if they dont include for example "US"?

As far as I understand I should use array_map right? But how do I tell it to remove those that doesn't include value instead of those who does include value I define?

You need to use array_filter()

$json['offers'] = array_filter($json['offers'], function($item){
    return $item['countries'][0]['code'] == "US";
});

Check result in demo

JavaScript, The task is to remove a JSON attribute from the JSON object. To do so, Here are few of the mostly used techniques discussed. First delete property need to be  The trim() method removes whitespace from both sides of a string. Note: The trim() method does not change the original string.

If I don't misunderstood your question, then simple array_filter() will do the trick for you.

<?php

$json = '[{"countries":[{"name":"United States","code":"US"}]},{"countries":[{"name":"United Kingdom","code":"UK"}]},{"countries":[{"name":"Canada","code":"CA"}]}]';
$decode = json_decode($json,1);
$new = array_filter($decode, function ($var) {
    return ($var['countries'][0]['code'] != 'US');
});
print_r($new);
?>

DEMO: https://3v4l.org/LtPEl

String.prototype.trim(), The trim() method returns the string stripped of whitespace from both ends. trim() does not affect the value of the str itself. Polyfill. Running the  I'd say you should trim the data on the PHP-side, as it's your PHP script that generates it 1. Basically : make sure your info.php script generates correct data. In PHP, you can use the trim() function. And if your data is stored in an array, you can apply that function to all items of that array using the array_map() function :

Thank you guys for answers, but I've did it in another way..

$salys2 = array("US");      
$salys = array_map('trim', $salys2);    


if (!in_array($offer['countries'][0]['code'], $salys)) {

    // skipping
    continue;
}

Could someone explain me why are we keeping [0] between countries and code?

Removing parameters outputted in JSON response. How to remove , When I make a GET request, the data is returned in JSON format. and then declare the {{data}} in the body, the response doesn't seem  Re: How to Trim Json Response and Parse the Data to the next Step in the Test Case @noegh_mmi , Was that the whole response? or just a string and you want to extract that value?

[Solved] Remove extra space in json string, This thread c# - csharp remove white spaces unless within quotes, ignoring escaped quotes[^] suggests using a JSON parser to do this for you  JSON stands for JavaScript Object Notation, a lightweight, an easily understandable format used for storing and transport data. It may look like a Jargon now, but it will not be a big deal once

How to remove null/empty JSON nodes from a request body , Here are the steps: 1. Download the latest version of the JSON-java library and place the "json-<data>".jar file to the <ReadyAPI>\bin\ext folder. Examples for handling JSON data in SQL Server 2016+ This post is a reference of my examples for processing JSON data in SQL Server. For more detailed explanations of these functions, please see my post series on JSON in SQL Server 2016: Part 1 — Parsing JSON Part 2 — Creating JSON Part 3 — Updating, Adding, and Deleting JSON Part 4 — JSON … Continue reading "The Ultimate SQL Server

JSONArray::remove() \ Language (API) \ Processing 3+, remove(). Examples. // The following short JSON file called "data.json" is parsed first element for (int i = 0; i < values.size(); i++) { JSONObject animal = values. The trim() method removes whitespace from both ends of a string. Whitespace in this context is all the whitespace characters (space, tab, no-break space, etc.) and all the line terminator characters (LF, CR, etc.).

Comments
  • array_map() is for changing (all) data. You want array_filter().
  • as @Dormilich you should use array_filter - var_dump(array_filter($offers, function($offer) { return ('US' === $offer['countries'][0]['code'] ?? null); }));
  • Possible duplicate of array_filter with assoc array?