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

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";

Related

How to parse a specific json array? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I'm a php beginner. I've got this kind of JSON ?
{
"rows":
[
["/page1.php","568"],
["/","78"],["/page2.php","4"],
["/page2.php","2"],
["/page3.php","1"],
["/search.php","1"]
]
}
to parse json i use
<?php
$json_file = file_get_contents('test.json');
$parsed_json = json_decode($json_file);
?>
But i don't know to parse each page and number. Anyone can help me on this surely basic issue ?
thanks a lot
$parsed_json will be an object, whose rows property is an array. The elements of this array are arrays, whose first element is the page name, the second element is the page number:
foreach ($parsed_json->rows as $page) {
$page_name = $page[0];
$page_number = $page[1];
echo "Page #" . $page_number . " on " . $page_name . "<br>";
}

Php string If contain echo that string [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
My Php mysql out put is array i need if string contain particular character i need print that string only
while($row2 = mysqli_fetch_array($result2)) {
echo $row2['link'];
}
Output is:
http://www.videoweed.es/file/b38300afda3e6
http://www.novamov.com/video/303a4428f6c6a
http://www.movshare.net/video/081aaa1356a6b
i need like this
if (strpos($a,'videoweed') !== false) {
echo $row2['link'];
}
it show out put is:
http://www.videoweed.es/file/b38300afda3e6
http://www.novamov.com/video/303a4428f6c6a
http://www.movshare.net/video/081aaa1356a6b
I need output only;
http://www.videoweed.es/file/b38300afda3e6
You have answer in your code. Just need to assemble it. check this:-
while($row2 = mysqli_fetch_array($result2)) {
if (strpos($row2['link'],'videoweed') !== false) {
echo $row2['link'];
}
}

Decoding Json string in 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
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.

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