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);
Related
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";
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
How can i do something like this?
class record
{
public $count;
}
$i = 0;
foreach($entry as $item) {
$i++;
$record$i = new record();
$record$i->count = $item['count'];
print $page$i;
}
Specifically, this part here is giving me errors.
$record$i = new record();
Note: the loop works fine if i just do print $item['count']
The error is: Parse error: syntax error, unexpected T_VARIABLE
see http://3v4l.org/lB4sR
${'record'.$i};
you can create a string that holds a varname and use it as a variable later - see the example
**Edit: but like #h2ooooooo sais in the comment to your question: use an array.
Hope is that what you asking, but I do not understand why to use such bad methods to count...
class record
{
static $countq = 0;
public function count(){
self::$countq++;
}
}
$entry = array(1, 4, 6, 7);
$obj = new record();
foreach($entry as $item) {
$obj->count();
}
echo record::$countq;
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.
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
The problem is that the $prenume echoing works, while $comentarii doesn`t. I tried with and without quotes. If I echo $comentarii in the while loop, it echoes. I only have 1 line of text to retreive from database. Please help! Thank you !
$gaseste_elevul = "SELECT prenume_elev, comentarii
FROM elevi
WHERE kod_utilizator=1";
$gaseste_elevul_query = mysql_query($gaseste_elevul);
while($elevul = mysql_fetch_array($gaseste_elevul_query))
{
$prenume = $elevul['prenume_elev'];
$comentarii = $elevul['comentarii'];
}
if($comentarii = NULL)
{
echo "Momentan nu aveti informari pentru $prenume!";
}
else
{
echo "Mai jos aveti informarile pentru $prenume:";
echo "$comentarii";
}//sfarsit else
I also tried to do the whole if($comentarii=NULL) in the while loop, to no result.
Change
if($comentarii = NULL)
to
if($comentarii == NULL)
Your first statement sets the $comentarii to null, and that's why it doesn't echoing anything.
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);
}