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
We have created some json as follows
{"deliveries":[{"cn":"1498","airlinetype":" Airbus A330-200 ","registration":" G-VYGK ","airline":"Thomas Cook Airlines","date":" 29. Apr 2015 "}]}
We are trying to loop through each delivery extracting the data to insert into the database. We ran the data through a json validator and it says everything is OK. Do we need to use json_encode to convert this as we are having problems trying to access each of the elements.
What would be the best approach to do this?
Thanks in advance
Rich
You should use json_decode() function. Please check solution below:
$json = '{"deliveries":[{"cn":"1498","airlinetype":" Airbus A330-200","registration":" G-VYGK ","airline":"Thomas Cook Airlines","date":" 29. Apr 2015 "}]}';
$data = json_decode($json);
foreach($data->deliveries[0] as $field=>$value) {
echo "{$field} = {$value}<br>";
//*** code to write to db follows here...
}
You want to decode the json string that you have posted.
A quick way since you know the name of each json branch is the following
$json = '{"deliveries":[{"cn":"1498","airlinetype":" Airbus A330-200 ","registration":" G-VYGK ","airline":"Thomas Cook Airlines","date":" 29. Apr 2015 "}]}';
$out = json_decode($json,true);
foreach($out["deliveries"] as $deliveries) {
echo $deliveries["cn"];
echo $deliveries["airlinetype"];
echo $deliveries["registration"];
echo $deliveries["airline"];
echo $deliveries["date"];
}
Check it out: http://sandbox.onlinephpfunctions.com/code/cfd8cdeb7f83536db4b4df74778243306c1c2d8c
Related
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 4 years ago.
Improve this question
I can't seem to get this correct.
I have this PHP command that I need to do the same thing in Python:
preg_match("/^(collabedge-|cb|ss)([0-9]+).dc-([0-9]+).com$/", $domain, $matches);
I have three possible string formats:
domain = collabedge-123.dc-01.com
domain = cb123.dc-01.com
domain = ss123.dc-01.com
I need to pull out the 123 and 01 from the string no matter what the format of the string and assign to variables.
You can use this code:
import re
domain = "collabedge-123.dc-01.com"
# preg_match("/^(collabedge-|cb|ss)([0-9]+).dc-([0-9]+).com$/", $domain, $matches);
regex = r"^(collabedge-|cb|ss)([0-9]+).dc-([0-9]+).com$"
res = re.match(regex,domain)
print(res.group(0))
print(res.group(1))
print(res.group(2))
print(res.group(3))
Output:
collabedge-123.dc-01.com
collabedge-
123
01
As sure as I posted the question I kept trying and figured it out. For those that want to know the answer.
d = re.match(r"(collabedge-|cb|ss)([0-9]+)\.dc-([0-9]+)\.com", domain)
firstnum = d.group(2)
secnum = d.group(3)
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 3 years ago.
Improve this question
So, I'm trying to make a simple web JSON API with data from my MySQL DB (10.0.35-MariaDB) I'm trying to use json_encode(); to output the information. It succeeds in gathering my data and outputting it only not in a JSON format?
Here is what's outputted compared to what I'm expecting;
What I have...
What I dream about having...
I think the fact that the JSON Formatter Chrome extension doesn't even pick up on my page sorta is a blatant sign that I'm not doing something right...
$conn = mysqli_connect('host', 'username', 'pass', 'db');
$query = mysqli_query($conn, "SELECT * FROM `table`;");
while ($row = mysqli_fetch_assoc($query)) {
$data[] = $row;
}
echo json_encode($data);
I know that to archive such a beautiful JSON file like steam's it'll require more work so for now, I'm just asking for help on actually outputting in JSON format.
What is being output is perfectly fine JSON. Your browser extension simply doesn't pick up on that fact because you're most likely not denoting the content type in the HTTP Content-Type header. Without that, anything is simply plaintext to the browser. Add this before outputting your JSON:
header('Content-Type: application/json');
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 5 years ago.
Improve this question
My value is coming like this with two enters. please see below :
125
124
132
I am getting this value by php variable and want to get values with commas in new php variable.
i want like this 125,124,132
anyone have an idea for that please?
$str = "125
124
132";
$str = str_replace("\r\n\r\n", ",", $str);
echo $str;
Try the above code. Please let know if this worked.
You can directly add commas between numbers using
number_format() function of PHP
<?php
echo number_format("125124132")."<br>";
?>
Answer:- 125,124,132
Try the above given code and let me know if this worked.....
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
I want to make a code who echo a users name and his avatar from steam, but i dont know how, i tryied this: http://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/?key=52A66B13219F645834149F1A1180770A&steamids=76561197960435530 but i dont know how to get just the username and the avatar from that info im getting, any help?
You must get the content of this file through file_get_contents and parse that json format to php array. After just get element personaname and avatar and use it with your HTML code
An example of how you could parse that json.
$json = file_get_contents('http://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/?key=52A66B13219F645834149F1A1180770A&steamids=76561197960435530');
$parsed = json_decode($json);
foreach($parsed->response->players as $player){
echo $player->personaname . '<br>';
echo "<img src='" . $player->avatar . "'>";
}
If you have any questions, feel free to ask!
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 8 years ago.
Improve this question
I have a simple php program which is getting the data from the url, but I want to check whether it contains a alphabet or not.
The data which I get from the url is as follows ?query=seller,12. I used ctype_alpha() for this, but I am getting no result for this. I have a rough idea that it can be done by preg_match() but I don't how to do it.
Please help me as I am beginner to php.
Thanks in advance
Here you go,
<?php
$subject = "?query=seller,12";
if(preg_match('/[a-zA-Z]/', $subject)){
echo "It has a alphabet";
}
?>
if you want to print all the characters from that string, you can use like this
preg_match_all('/[a-zA-Z]/', $subject,$matches);
print_r($matches);
$matches is an array of all available matches of the specified pattern
Try this dude.
if (!preg_match('/[^A-Za-z]/', $string)) // '/[^a-z\d]/i' should also work.
{
// string contains only english letters
}