Skip to content
Advertisement

Array filter JSON data with price parameter

I have this JSON data and I am using this code to match the title. It works, but now I want to go one step further and list all products which are less than some price, or more than some price, or equal to some price

$json = '[{
    "id": 2042049298481,
    "title": "Test Product 53",
    "variants": [{
        "id": 15733087797297,
        "title": "Default Title",
        "price": "100.00",
        "sku": "no"
    }]
}, {
    "id": 2196682342449,
    "title": "its test yar",
    "variants": [{
        "id": 16385289879601,
        "title": "Default Title",
        "price": "144.00",
        "sku": "no"
    }]
}, {
    "id": 4175970041905,
    "title": "uhjkjhkhk",
    "variants": [{
        "id": 30302326554673,
        "title": "Default Title",
        "price": "1000.00",
        "sku": "10"
    }]
}, {
    "id": 4183828791345,
    "title": "kaminsss",
    "variants": [{
        "id": 30346449518641,
        "title": "Default Title",
        "price": "111.00",
        "sku": "no"
    }]
}, {
    "id": 4247884890161,
    "title": "hellooo",
    "variants": [{
        "id": 30579860832305,
        "title": "Default Title",
        "price": "1111.00",
        "sku": "no"
    }]
}, {
    "id": 4248143822897,
    "title": "10oct latest collection",
    "variants": [{
        "id": 31157780938801,
        "title": "50",
        "price": "111.00",
        "sku": "no-1"
    }, {
        "id": 31157802270769,
        "title": "60",
        "price": "101.00",
        "sku": ""
    }, {
        "id": 31157804761137,
        "title": "70",
        "price": "111.00",
        "sku": ""
    }]
}, {
    "id": 4284600746033,
    "title": "18oct2019",
    "variants": [{
        "id": 31595410030641,
        "title": "120 / black",
        "price": "100.00",
        "sku": "10"
    }]
}, {
    "id": 4285596860465,
    "title": "test2222",
    "variants": [{
        "id": 30877916921905,
        "title": "Default Title",
        "price": "111.00",
        "sku": "no"
    }]
}]';
$array = json_decode($json,1);
$filter_name ='title';
$filter_value ='Test';
$expected88 = array_filter($array, function($el) use ($filter_value, $filter_name) {
       return ($el[$filter_name] >= $filter_value);

}); 

echo json_encode($expected88,true);

This works flawlessly for title but we want to filter based on variant price. There may be several prices for one variant and if $filter_value is greater than, equal to, or less than, anyone can occur.

For example user may search for product whose minimum price is 50, so in that case, if any variant is matching criteria then it will list those products, or user may search that all variants’ prices should be less than 50.

We tried like this

$expected = array_filter($array, function ($var) use ($filter_value) {
    return ($var[$filter_name] >= $filter_value);
});

but it did not work. We tried foreach and tested again but it always gave empty result

Advertisement

Answer

Here is possible solution

$filter_name ='variants';
$filter_value = 200;
$filter_field = "price";


$expected88 = array_filter($array, function($el) use ($filter_value, $filter_name, $filter_field) {
       return ((int)$el[$filter_name][0][$filter_field] >= (int)$filter_value);
}); 
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement