Decoding Json string in PHP [closed] - php

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
My Json encoded output is like the below one
{
"msg_id":"14789",
"message":"dummy+message",
"msgType":"TEXT",
"sendondate":"2013-12-26 13:19:49",
"seq_id":{
"1":{
"valid":"true",
"credit":"1.00",
"linecount":1,
"billcredit":1,
"id_provider":"18",
"providerkey":"TI",
"regionKey":"CH",
"originalnumber":"11",
"validnumber":"+11",
"countryprefix":"11",
"ONLYNUMBER":"11",
"NUMBERWITHZERO":"11",
"INTERNATIONALONLY":"11",
"INTERNATIONALWITHPLUS":"+11",
"mnpID":"905",
"dlr_seq":1,
"textMessage":"dummy+message",
"status":"",
"remarks":""
}
}
}
I want to print the value for billcredit as output. How can I decode this in php?

Try like this :
$json = '{"foo-bar": 12345}';
$obj = json_decode($json);
print $obj->{'foo-bar'};
For more about json in php visit the link.

like this
var_dump(json_decode($json));

this is what you want to get with json_decode()
$string = '{"msg_id":"14789","message":"dummy+message","msgType":"TEXT","sendondate":"2013-12-26 13:19:49","seq_id":{"1":{"valid":"true","credit":"1.00","linecount":1,"billcredit":1,"id_provider":"18","providerkey":"TI","regionKey":"CH","originalnumber":"11","validnumber":"+11","countryprefix":"11","ONLYNUMBER":"11","NUMBERWITHZERO":"11","INTERNATIONALONLY":"11","INTERNATIONALWITHPLUS":"+11","mnpID":"905","dlr_seq":1,"textMessage":"dummy+message","status":"","remarks":""}}}';
// Return Object Data
print_r( json_decode($string) );
// Return Array Data
print_r( json_decode($string, true) );
$decoded_data = json_decode($string, true);
// Bill Credit Value
echo "billcredit: " . $decoded_data['seq_id'][1]['billcredit'];
http://codepad.org/BJ5KbHVq

$your_data_in_array_comes_here = json_decode("Your Output String");
With above array you can generate html as your wish.

Related

how to check if user is in JSON array [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
How Can I check if "User" is inside this json:
{
players: [
"u",
"us",
"use",
"user",
"users"
]
}
this might work, using json_decode:
$json ='{"players": ["u","us","use","User", "users"]}';
$playerlist = json_decode($json, true);
if(in_array("user", $playerlist['players']))
echo "'user' found in players";
else
echo "'user' not found in players";
You can convert json string to PHP array with json_encode, But you need to add "" on the index of the json string, like "players" in you code, then search the string in the array with in_array.
$jsonString = '{"players": ["u","us","use", "user", "User", "users"]}';
$arr = json_decode($jsonString, true);
if(in_array("User", $arr['players']))
echo "'User' is in the players\r\n";
else
echo "'User' is NOT in the players\r\n";

PHP Simple HTML DOM Getting ONLY first 5 links inside a div class [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
So far I have this
<?PHP include('simple_html_dom.php');
$html = file_get_html('http://www.mangastream.com/');
foreach($html->find('.side-nav') as $t)
foreach($t->find('a')as $k)
echo $k->href . '<br>';
?>
which outputs all the links from inside the class. but I just want to have the first 5 links.
find() returns an array, you can do a single find operation instead of two, and you can slice an array to the first five elements by using array_slice.
This allows you to get the first five element easily:
$ks = $html->find('.side-nav a');
foreach (array_slice($ks, 0, 5) as $k)
echo $k->href, '<br>'
;
However I suggest you take the DOMDocument based HTML parser - perhaps in compbination with SimpleXML so that you can run xpath queries on the document instead.
try that
<?PHP include('simple_html_dom.php');
$html = file_get_html('http://www.mangastream.com/');
foreach($html->find('.side-nav') as $t){
foreach($t->find('a')as $key => $k){
echo $k->href . '<br>';
if($key >= 4){
break;
}
}
}
?>

How do I use received JSON with PHP [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I'm reading an integration API and it says it will send requests in this format:
content-type: application/json
accept: application/json
x-gsc-api-key: lRDs8NfsVTmHD8SC1234
{
"name" : "testuser",
"password" : "098f6bcd4621d373cade4e832627b4f6",
"reference" : "b86de61-af20-4c47-af9a-6f2edeebc4fe"
}
I want to server the requests with PHP. I know some other posts also ask something along the same lines, but they were sending the request via AJAX method. I don't know which method the request is going to be sent.
Can I just reference the variables $_POST['name'] and $_POST['password'] to get the values? Or do I need to call json_decode($_POST)?
Is this information enough the write a PHP script that server the request?
Any help would be appreciated.
No, you cannot use $_POST directly. You will have to manually extract the response body, json_decode it and work on the results. But it's easy to do:
$data = json_decode(file_get_contents('php://input'));
echo $data->name;
$json = file_get_contents('php://stdin');
You have to use the json_decode() function for this (http://php.net/manual/en/function.json-decode.php)
No - you can't - however you can convert the JSON to $_POST like so:
<?php
$_POST = array_merge($_POST, json_decode(file_get_contents("php://stdin"), true));
?>
Not sure if i read the question correctly but >.>
$json = '{"name":"testuser","password":"098f6bcd4621d373cade4e832627b4f6","reference":"b86de61-af20-4c47-af9a-6f2edeebc4fe"}';
$data = #json_decode($json);
$name = $data->name;
$password = $data->password;
$ref = $data->reference;
echo "${name} ${password} ${ref}";
or use the true like below. . .
$json = '{"name":"testuser","password":"098f6bcd4621d373cade4e832627b4f6","reference":"b86de61-af20-4c47-af9a-6f2edeebc4fe"}';
$data = #json_decode($json, true);
$name = $data['name'];
$password = $data['password'];
$ref = $data['reference'];
echo "${name} ${password} ${ref}";

How to update/edit a JSON file using PHP [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Here is my JSON
[
{
"activity_code":"1",
"activity_name":"FOOTBALL"
},
{
"activity_code":"2",
"activity_name":"CRICKET"
}
]
I need to update {"activity_code":"1","activity_name":"FOOTBALL"} to {"activity_code":"1","activity_name":"TENNIS"} based on activity_code
How can I achieve this in PHP?
First, you need to decode it :
$jsonString = file_get_contents('jsonFile.json');
$data = json_decode($jsonString, true);
Then change the data :
$data[0]['activity_name'] = "TENNIS";
// or if you want to change all entries with activity_code "1"
foreach ($data as $key => $entry) {
if ($entry['activity_code'] == '1') {
$data[$key]['activity_name'] = "TENNIS";
}
}
Then re-encode it and save it back in the file:
$newJsonString = json_encode($data);
file_put_contents('jsonFile.json', $newJsonString);

Str replace function [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I want to censor words from a form string, and control the censor words with sql database. Here's what I have so far:
while ($bad_w = mysql_fetch_array($result_badw)) {
$newmessage = str_replace($bad_w['word'],"****",$org);
}
Any ideas on how to correct it?
You are repeatedly using the original string in your replacement. You should be overwriting the string
($org = str_replace($bad_w['word'],"****",$org)) to ensure all words are filtered.
Just hope nobody talks about Kuroshitsuji on your forum :p
You overwrite $newmessage each time you are replacing new word.
You should also use str_ireplace(), which is case-insensitive - that will be better for censorship.
Try like this:
$newmessage = $org;
while($row = mysql_fetch_array($result_badw)) {
$newmessage = str_ireplace($row['word'], "****", $newmessage);
}
Or, if you want the same number of * as are letters in the bad word:
$newmessage = $org;
while($row = mysql_fetch_array($result_badw)) {
$bword = $row['word'];
$repl = str_repeat('*', strlen($bword));
$newmessage = str_ireplace($bword, $repl, $newmessage);
}

Categories