-1

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.

Eddie
  • 26,593
  • 6
  • 36
  • 58
AD Tee
  • 345
  • 4
  • 21

1 Answers1

1

The name of the variable to use inside the function is the name used on the parameter, not the name of the actual array, which is not in scope inside the function

function attribute_service($var)
{
    //return($data_Category_Main['attribute_group_name'] == 'Service');
    return($var['attribute_group_name'] == 'Service');
    //     ^^^^  the changed code
}

Example code:

<?php
$j = '[{
  "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"
}]';

function attribute_service($var)
{
    return($var['attribute_group_name'] == 'Service');
}

$data_Category_Main = json_decode($j, true);
$new = array_filter($data_Category_Main, "attribute_service");
//print_r($new);

foreach($new as $row):
?>
    <div>
        <input class="styled-checkbox" style="display:inline-block;" id="styled-checkbox-1" type="checkbox" value="value1">
        <label><?= $row['attribute_name']; ?></label>
    </div>
<?php    
endforeach;

RESULTS

<div>
    <input class="styled-checkbox" style="display:inline-block;" id="styled-checkbox-1" type="checkbox" value="value1">
    <label>Cash On Delivery</label>
</div>
<div>
    <input class="styled-checkbox" style="display:inline-block;" id="styled-checkbox-1" type="checkbox" value="value1">
    <label>Shipping</label>
</div>
<div>
    <input class="styled-checkbox" style="display:inline-block;" id="styled-checkbox-1" type="checkbox" value="value1">
    <label>Insurance</label>
</div>
RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
  • I tried to apply your code but it seem like does not work. Do u have any idea? – AD Tee Apr 22 '19 at 13:14
  • It works for me! And returned `Cash On Delivery Shipping Insurance` – RiggsFolly Apr 22 '19 at 13:22
  • Hi Riggs, thanks for your effort, if using your method it will work perfectly. But when I called from API, it will hit error `json_decode() expects parameter 1 to be string`, `array_filter() expects parameter 1 to be array`... so I edited my code to `$url_Category_Main = "https://api.abc123.com/category/main"; $encoded_url_Category_Main = json_encode(file_get_contents($url_Category_Main), true); $j = json_decode($encoded_url_Category_Main);` – AD Tee Apr 22 '19 at 13:47
  • I got my answer and posted in comment above. Thanks Riggs for your time and guidance – AD Tee Apr 22 '19 at 13:53