Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

php get values from json encode

Tags:

json

php

I have an url passing parameters use json_encode each values like follow:

$json = array (     'countryId' => $_GET['CountryId'],     'productId' => $_GET['ProductId'],     'status'    => $_GET['ProductId'],     'opId'      => $_GET['OpId'] );  echo json_encode($json); 

It's returned a result as:

{     "countryId":"84",   "productId":"1",   "status":"0",   "opId":"134" } 

Can I use json_decode to parse each values for further data processing?

Thanks.

like image 483
conmen Avatar asked Sep 14 '12 17:09

conmen


People also ask

How can access JSON encode data in PHP?

To receive JSON string we can use the “php://input” along with the function file_get_contents() which helps us receive JSON data as a file and read it into a string. Later, we can use the json_decode() function to decode the JSON string.

Can you JSON encode an object in PHP?

json_encode() is a native PHP function that allows you to convert PHP data into the JSON format. The function takes in a PHP object ($value) and returns a JSON string (or False if the operation fails).

What is JSON encode in PHP?

The json_encode() function is used to encode a value to JSON format.

How do I decode a JSON file?

You just have to use json_decode() function to convert JSON objects to the appropriate PHP data type. Example: By default the json_decode() function returns an object. You can optionally specify a second parameter that accepts a boolean value. When it is set as “true”, JSON objects are decoded into associative arrays.


1 Answers

json_decode() will return an object or array if second value it's true:

$json = '{"countryId":"84","productId":"1","status":"0","opId":"134"}'; $json = json_decode($json, true); echo $json['countryId']; echo $json['productId']; echo $json['status']; echo $json['opId']; 
like image 143
Mihai Iorga Avatar answered Oct 07 '22 10:10

Mihai Iorga