I using php to get an array from JSON api. How can I filter this array based on selected column values and return filtered array?
Here is my sample JSON data:-
[{
"attribute_group_name": "Location",
"attribute_name": "Local",
"item_id": "1111"
}, {
"attribute_group_name": "Service",
"attribute_name": "Cash On Delivery",
"item_id": "0000"
}, {
"attribute_group_name": "Service",
"attribute_name": "Shipping",
"item_id": "3333"
}, {
"attribute_group_name": "Service",
"attribute_name": "Insurance",
"item_id": "4444"
}]
In abc.php, I tried to use array_filter but it does not work.
Here is my code in abc.php
<?php
$url_Category_Main = "https://api.abc123.com/category/main";
$data_Category_Main = json_decode(file_get_contents($url_Category_Main), true);
function attribute_service($var)
{
return($data_Category_Main['attribute_group_name'] == 'Service');
}
$new = array_filter($data_Category_Main, "attribute_service");
$count=0;
foreach($new as $row_Category_Main):
?>
<div>
<input class="styled-checkbox" style="display:inline-block;" id="styled-checkbox-1" type="checkbox" value="value1">
<label><?= $row_Category_Main['attribute_name']; ?></label>
</div>
<?php endforeach; ?>
In this case, how can I getting an array that filtered by attribute_group_name = Service and return value in label ??
Expected Result:
Cash On Delivery
Shipping
Insurance
Please help. Thank you.