0

I am trying to get only these metafields objects with namespace="global", how can I do that in PHP ?

<?php $response = {"metafields":[{"id":30007223558,"namespace":"global"}, {"id":454872458451,"namespace":"local"}, {"id":154644565,"namespace":"global"}]} ?>
<?php $response = json_decode($response); ?>

1 Answers1

4
$response = json_decode($response, true);

$filtered['metafields'] = array_filter($response['metafields'], function ($item) {
   return $item['namespace'] === 'global'; 
});

var_dump($filtered);

array(1) {
  ["metafields"]=>
  array(2) {
    [0]=>
    array(2) {
      ["id"]=>
      int(30007223558)
      ["namespace"]=>
      string(6) "global"
    }
    [2]=>
    array(2) {
      ["id"]=>
      int(154644565)
      ["namespace"]=>
      string(6) "global"
    }
  }
}
Vini
  • 627
  • 5
  • 18