0

This is the target jason string:

{"dpid": 272, "priority": 10, "match": {"nw_src": "192.168.1.1", "nw_dst": "192.168.1.2", "nw_proto": 1, "eth_type": 0x0800}, "actions":[{"type": "DROP"}]}

This is the php array that i made:

$rule = array(
  "dpid" => 272,
  "priority" => 10,
  "match" => {"nw_src": "$_POST['src']", "nw_dst": "$_POST['dst']", "nw_proto": 1, "eth_type": 0x0800},
  "actions"=> [{"type": "DROP"}],  
);

i am trying to turn this array into the json string above, using:

$data_string=json_encode( $rule );

but it doesnt work :(

i know the array is really non-sense, i am really new to php. Could somebody help me?

1
  • 1
    Can you elaborate a bit on "but it doesn't work"? Are you getting an error? The wrong results? Commented Nov 29, 2015 at 8:10

1 Answer 1

3

Your array should be:

$rule = array(
    "dpid" => 272,
    "priority" => 10,
    "match" => array(
        "nw_src" => $_POST['src'], 
        "nw_dst" => $_POST['dst'], 
        "nw_proto" => 1, 
        "eth_type" => 0x0800 
    ),
    "actions"=> array(array("type" => "DROP")),  
);

After that json_encode function will do all the work for you:

$data_string = json_encode($rule);
Sign up to request clarification or add additional context in comments.

1 Comment

op's actions is an array of object not object itself.

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.