Question: I have a structure that some of the values will get Integer or string so I used this structure in my structure to parse JSON in the correct way but the problem is that when I want to print the value it will print string("1") instead of 1. 

```
 public struct  player1_result_sheet : Decodable {
    let ans_1 : QuantumValue?
    let ans_2 : QuantumValue?
    let ans_3 : QuantumValue?
    let ans_4 : QuantumValue?
}
 enum QuantumValue: Decodable {

    case int(Int), string(String)

    init(from decoder: Decoder) throws {
        if let int = try? decoder.singleValueContainer().decode(Int.self) {
            self = .int(int)
            return
        }
        if let string = try? decoder.singleValueContainer().decode(String.self) {
            self = .string(string)
            return
        }
        throw QuantumError.missingValue
    }
    enum QuantumError:Error {
        case missingValue
    }
}

```

Here is the printing after decode: 

```
(self.res?.response?.detailData?[indexPath.row].player1_result_sheet?.ans_1!)!

```

Answer: 
The code you provided seems to be working, however, if you just want the keys of the associative array as the output you can use `array_keys` to get the keys of your filtered result.

```
$test = Array(
    8708 => Array('ref_id' => 93939),
    8709 => Array('ref_id' => 116234),
    8710 => Array('ref_id' => 116234)
);
$filtered = array_filter($test, function($v) { return $v['ref_id'] == '116234'; });
print_r(array_keys($filtered)); // array_keys to get the keys from filtered

```

**Output:**

```
Array ( [0] => 8709 [1] => 8710 )

```

**EDIT:** The issue that you're having with your updated question is that `$test` cannot be seen within filter's anonymous function, and so you need to use the `use` keyword to give access to it. Read more about `use` [here](https://stackoverflow.com/questions/8403908/accessing-outside-variable-using-anonymous-function-as-params).

Working code:

```
$test = Array(
    8708 => Array('ref_id' => 93939),
    8709 => Array('ref_id' => 116234),
    8710 => Array('ref_id' => 116234)
);

$data['attr'][8] = '116234';

$filtered = array_filter($test, function($v) use (&$data) { 
    return $v['ref_id'] == $data['attr'][8]; 
});

print_r(array_keys($filtered));

```

**Output:**

```
Array ( [0] => 8709 [1] => 8710 )

```
Your code doesn't work because `$data` is not in scope in your anonymous function. If you had PHP error reporting enabled you would have seen 3 error messages like this:

> 
> Notice: Undefined variable: data in /in/RNpEd on line nnn
> 
> 
> 

You need to rewrite your code like this (note the `use ($data)` in the definition of the anonymous function):

```
$test = array('8708' => array('ref_id' => '93939'), '8709' => array('ref_id' => '116234'), '8710' => array('ref_id' => '116234'));
$data['attr'][8] = '116234';
$filtered = array_filter($test, function($v) use($data) { return $v['ref_id'] == $data['attr'][8]; });
print_r($filtered);

```

Output:

```
Array (
    [8709] => Array (
        [ref_id] => 116234
    )
    [8710] => Array (
        [ref_id] => 116234
    )
)

```

[Demo on 3v4l.org](https://3v4l.org/fPIVo)