-1

I am getting this output:

{"params":{"2":true,"3":true}}

I have printed this as (at PHP side):

$data = file_get_contents("php://input");
        print($data);

I want to use that 2 and 3 values using loop at PHP side. But as I am trying, it is not json format (its doubt). So how can i use these values using loop?

VBMali
  • 1,360
  • 3
  • 19
  • 46

3 Answers3

3
$j='{"params":{"2":true,"3":true}}';

$decoded=json_decode($j,1);

print "<pre>\n";
print_r($decoded);
print "</pre>\n";

The second parameter of json_decode() is

'assoc' - When TRUE, returned object s will be converted into associative array

KevInSol
  • 2,560
  • 4
  • 32
  • 46
2
<?php

$json='{"params":{"2":true,"3":true}}';

$params=json_decode($json, true)['params'];

foreach ($params as $k =>$v){
    echo $k . ' is ' .  var_export($v, true) . PHP_EOL;
}

output:

2 is true
3 is true
Ali
  • 2,993
  • 3
  • 19
  • 42
0
$data='{"params":{"2":true,"3":true}}';
$decoded=json_decode($data,1);
        foreach ($decoded as $key => $param)
        {
            foreach ($param as $user_id => $value)
            {
                    echo $user_id;
            }
        }

It gives me output:

 2 3

(as I expected).

VBMali
  • 1,360
  • 3
  • 19
  • 46