-1

this is my result $dataJson like this

$dataJson :

[
    {
        "42773": "2021-03-01",
        "jumlah_bayar": 712584
    },
    {
        "42792": "2021-03-01",
        "jumlah_bayar": 795000
    },
    {
        "42805": "2021-03-01",
        "jumlah_bayar": 1536000
    },
    {
        "42887": "2021-03-01",
        "jumlah_bayar": 711566
    },
    {
        "42773": "2021-03-01",
        "jumlah_bayar": 712584
    },
    {
        "42792": "2021-03-01",
        "jumlah_bayar": 795000
    }
]


$filter = [42792, 42805];

How to remove object whose key in date value is not available in $filter ? for example here I want to delete keys 42792 and 42805 in the form of an array in $filter

I've already tried, but there is still data left using this way :

(array)$filter= [42792];
      for($i=0;$i<count($dataJson);$i++) {
        foreach($dataJson[$i] as $key => $value) {
          if(!in_array($key, $tmp_inv_id)) {
            unset($dataJson[$i]);
          }
        }
      }
4
  • Have you tried anything so far? Commented Jun 7, 2021 at 6:48
  • @NigelRen of course, i've changed my question with the thing i tried Commented Jun 7, 2021 at 7:05
  • Does this answer your question? PHP - How to modify deeply nested associative arrays? Commented Jun 7, 2021 at 7:16
  • No, I want to filter data with unset data where not in array $filter Commented Jun 7, 2021 at 7:40

1 Answer 1

0

Instead of writing your own loops, you can use array_filter with a custom callback function for stuff like this. Make sure you decode the JSON so that it creates associate arrays out of the objects, that makes accessing the first key easier.

$data = json_decode('[
    {
        "42773": "2021-03-01",
        "jumlah_bayar": 712584
    },
    {
        "42792": "2021-03-01",
        "jumlah_bayar": 795000
    },
    {
        "42805": "2021-03-01",
        "jumlah_bayar": 1536000
    },
    {
        "42887": "2021-03-01",
        "jumlah_bayar": 711566
    },
    {
        "42773": "2021-03-01",
        "jumlah_bayar": 712584
    },
    {
        "42792": "2021-03-01",
        "jumlah_bayar": 795000
    }
]', 1);

$filter = [42792, 42805];

$data = array_filter($data, function($item) use ($filter) {
  return !in_array(array_key_first($item), $filter);
});
var_dump($data);

Result:

array(3) {
  [0]=>
  array(2) {
    [42773]=>
    string(10) "2021-03-01"
    ["jumlah_bayar"]=>
    int(712584)
  }
  [3]=>
  array(2) {
    [42887]=>
    string(10) "2021-03-01"
    ["jumlah_bayar"]=>
    int(711566)
  }
  [4]=>
  array(2) {
    [42773]=>
    string(10) "2021-03-01"
    ["jumlah_bayar"]=>
    int(712584)
  }
}
Sign up to request clarification or add additional context in comments.

2 Comments

I got error ErrorException: json_decode() expects parameter 1 to be string, array given in file. when I try return json_decode($tmp_data,1);
Well then your $tmp_data obviously is not JSON, but a PHP array structure already. (But if the items are not associative arrays yet, but objects, then you will need to encode it as JSON first, and then decode again with the second parameter set to true - otherwise, using array_key_first to dynamically get the name of the first key won’t work.)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.