Viewed   92 times

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.

 Answers

4

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'];
Monday, October 24, 2022
5

Try something like this:

//initialize array
$myArray = array();

//set up the nested associative arrays using literal array notation
$firstArray = array("id" => 1, "data" => 45);
$secondArray = array("id" => 3, "data" => 54);

//push items onto main array with bracket notation (this will result in numbered indexes)
$myArray[] = $firstArray;
$myArray[] = $secondArray;

//convert to json
$json = json_encode($myArray);
Friday, December 23, 2022
4

. is used for to access object property while [] notation is used for to access array value.

console.log(data[0]['request_id']);

Demo

Update:

Try with -

var response = jQuery.parseJSON(data0)
console.log(response.request_id);
Saturday, September 10, 2022
3

Try something like this:

$item_array = json_decode($json, true);
$insert_query = "INSERT INTO items (column1,column3) VALUES ";
$i = 0;
$array_count = count($item_array);

foreach($item_array as $item) {
 $end = ($i == $array_count) ? ';' : ',';
 $insert_query .= "('".$item['date']."','".$item['device_id']."')" . $end;
 $i ++;
}

mysql_query($insert_query);

The resulting query will look something like:

INSERT INTO items (column1,column3) VALUES ('asdf',123),('asdfe',1234);
Tuesday, November 22, 2022
 
coomie
 
4

you can use it like this, in JSON format when you evaluate false value it will give you blank, and when you evaluate true it will give you 1.

$str = '[{"clientId":"17295c59-4373-655a-1141-994aec1779dc","channel":"/meta/connect","connectionType":"long-polling","ext":{"fm.ack":false,"fm.sessionId":"22b0bdcf-4a35-62fc-3764-db4caeece44b"},"id":"5"}]';

$arr = json_decode($str,true);

if($arr[0]['ext']['fm.ack'])    // suggested by **mario**
{
    echo "true";    
}
else {
    echo "false";   
}
Thursday, October 6, 2022
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :