Conditional extraction of JSON data to CSV file

I have a JSON file which I need to convert to CSV using PHP. However, only certain records from the JSON file should be converted based on whether they match certain conditions. In the case the JSON data is financial transactions, and only those transactions which match certain merchant id’s should be converted.

I have extracted the JSON data into an array in PHP but not sure how to condition match the records which should then be converted to csv.

<?php

$raw_data = file_get_contents('transactions.json');

$decoded_json = json_decode($raw_data, true);

    foreach($decoded_json as $row) {
        foreach($row['payee'] as $k) {
        print_r($row);
        echo "<pre>";
        echo $k;
        echo "</pre>";
    }
}

 $file = fopen("output.csv", "a");

foreach($decoded_json as $row){
     fputcsv($file, $row);
 }

 fclose($file);
?>