I'm trying to get the values of teams in the Json,I gave tried the code below but it returns NULL, Any ideas?
<?php
$url = 'http://bristolrugby.matchdaylive.com/tools/ajax/cache.php?';
$url.= 'type=Fixture&format=json&TeamId=25&Source=sfms&module=StatsRugbyMDS&CompSeason=2015&project=bristol';
$response = json_decode(file_get_contents($url));
$fixture_array = $response;
var_dump ($fixture_array->SoticFeed->Fixtures->Fixture[0]->Teams->Team);
echo '<br/><hr>';
$fix = $fixture_array->SoticFeed->Fixtures;
foreach($fix as $fix){
print $fix->Fixture->Teams->Team->TmnmDisplay . "<hr>";
}
?>
It seems like you got confused by the structure of the data. Your final loop is wrong, I think it should look like this:
$fix = $fixture_array->SoticFeed->Fixtures->Fixture;
foreach($fix as $fix_detail){
foreach($fix_detail->Teams->Team as $team){
print $team->TmnmDisplay . "<hr>";
}
}
This is assuming you want to display all team's names. If this is not what you intended please let me know.
hello your Json response return array in array in array and this mean you have to loop over each key , let me explain you more
if you
print_r($response["SoticFeed]")
You will get Array in Array , now again
print_r($response["SoticFeed"]["Fixtures"])
again You will get Array , now the last part
foreach($response["SoticFeed"]["Fixtures"]["Fixture"] as $key=>$value){
print_r($value)
}
Related
I'm having some problems getting the data through my PHP loop.
<?php
$url = 'https://www.fibalivestats.com/data/1653309/data.json';
$content = file_get_contents($url);
$json = json_decode($content, true);
?>
<?php
foreach($json['totallds']['sPoints'] as $item); {
echo $item;
}
?>
The error I'm getting is an array to string conversion error. What I'm trying to get is the data from the sPoints array that will give me a Top 5 points scorers for a basketball game.
I'll build a table for this in HTML later but for now, it's not displaying the data at all and I'm getting errors. I feel like I may have confused arrays and strings too. Any thoughts about what I'm doing wrong? JSON file can be found in the $url variable.
Also if it helps, here's the link to where I have gotten the data from and what context the Top 5 is from https://www.fibalivestats.com/u/NSS/1653309/lds.html
Thanks!
Your $item is an array, so you can't just echo it like that. You can, however, echo its columns, for example:
foreach($json['totallds']['sPoints'] as $item) {
echo $item['firstName'] . ' ' . $item['familyName'];
}
Notice the removed semicolon between the foreach () and {.
Well, array to string conversion error means you're trying to echo an array.
If you see the return of the url you are looking at, you can verify that the "sPoints" key returns an array with several objects.
Try to change echo to print_r or var_dump to view entire data or complete your business logic.
Try changing:
echo $item;
to:
var_dump($item);
below is my 'script.json' file with json array and i want the values of webUserid and webPassword
{
"totalSize":2,
"webUserId":"abc",
"webPassword":"def",
"operation":"send",
"testMode":true,
"records":[
{
"phoneNumber":"1908908399",
"message":"Happy Birthday",
"Id":"a0YL0000008QYunMAG",
"deviceId":"ABCDEFXABCDEF"
}
]
}
I tried below one but not getting the result
<?php
$jsonString=file_get_contents("script.json");
$decoded=json_decode($jsonString,true);
foreach($decoded->data as $name){
echo $name->totalSize;
}
?>
Zarif, try the below code, its working 100%......... :)
<?php
$jsonString=file_get_contents("script.json");
$decoded=array(json_decode($jsonString,true));
foreach($decoded as $name){
echo $name['totalSize'];
}
?>
There's no need for a foreach loop in your current example, as you only have one level.
Your main problem is you're trying to use the result of your json_decode as an object when you've previously stated you want the result as an associative array.
The 2nd param of json_decode specifies what format the return value is in, so as you've done
$decoded = json_decode($jsonString, true);
you'll receive an array, which you could access like
echo $decoded['totalSize'];
if you wanted to treat it as on object as you've done in your question, either state false or omit a 2nd param in json_decode (it's false by default anyway), and that'll let you do what you're trying to do:
$decoded = json_decode($jsonString);
$decoded->totalSize;
lets say that
$myJSON = yourPostedJSON.
$myJSON = json_decode($myJSON, true);
echo $myJSON['webUserId'];
echo $myJSON['webPassword'];
I am really struggling with arrays in general but I can't seem to do anything with this: https://www.easycron.com/document/list
I want to be able to access each "field". For example the [cron_job_name] for [2]. So in that example the result I'd be looking for is "Send newsletters".
I have been messing around all night long but I think the thing I am not getting past is that it has the [status] => success [cron_jobs] => Array at the top.
So far the code I have is this:
$json = file_get_contents("https://www.easycron.com/rest/list?token=[MY KEY]&sortby=cronId&order=desc");
$myArray = json_decode($json,true);
echo "<pre>";
print_r($myArray);
echo "</pre>";
All this really does though is gets me a copy of what is on that page.
if you are able to print_r the array response, then you can cycle
if($myArray['status'] ==='success') {
$crons = $myArray['cron_jobs'];
foreach($crons as $cron){
if($cron['cron_job_name'] == 'Send newsletters') {
// Do your proccessing
break;
}
}
}
I have a problem to get the value from json data inside array when the data is more than one.
When my data only one like this:
$mydata='[{"firstName":"Ana","height":5.3}]';
I can just access the height of Ana by substring-ing it first and the decode it, like this:
$mydata= substr($mydata, 1, -1);
$obj = json_decode($mydata);
print $obj->{'height'};
The problem is when the data look like this:
$mydata='[{"firstName":"Ana","height":5.3},{"firstName":"Taylor","height":5.11}]';
How can I get the height of Ana?
print $obj->{0}->{'height'}; //doesn't work.
Please help. Thankyou in advance.
use json_decode
$b_arr=json_decode($mydata,true);
$b_arr[0]['height'];//0 is index for array
You can convert with json_decode and iterate through your array to get your specific data:
<?php
$mydata='[{"firstName":"Ana","height":5.3},{"firstName":"George","height":7.3}]';
$json = json_decode($mydata, true);
foreach($json as $key => $value) {
if($value['firstName'] == 'Ana') {
echo $value['height'];
break;
}
}
?>
I've have this list of products in JSON that needs to be decoded:
"[{"productId":"epIJp9","name":"Product A","amount":"5","identifier":"242"},{"productId":"a93fHL","name":"Product B","amount":"2","identifier":"985"}]"
After I decode it in PHP with json_decode(), I have no idea what kind of structure the output is. I assumed that it would be an array, but after I ask for count() it says its "0". How can I loop through this data so that I get the attributes of each product on the list.
Thanks!
To convert json to an array use
json_decode($json, true);
You can use json_decode() It will convert your json into array.
e.g,
$json_array = json_decode($your_json_data); // convert to object array
$json_array = json_decode($your_json_data, true); // convert to array
Then you can loop array variable like,
foreach($json_array as $json){
echo $json['key']; // you can access your key value like this if result is array
echo $json->key; // you can access your key value like this if result is object
}
Try like following codes:
$json_string = '[{"productId":"epIJp9","name":"Product A","amount":"5","identifier":"242"},{"productId":"a93fHL","name":"Product B","amount":"2","identifier":"985"}]';
$array = json_decode($json_string);
foreach ($array as $value)
{
echo $value->productId; // epIJp9
echo $value->name; // Product A
}
Get Count
echo count($array); // 2
Did you check the manual ?
http://www.php.net/manual/en/function.json-decode.php
Or just find some duplicates ?
How to convert JSON string to array
Use GOOGLE.
json_decode($json, true);
Second parameter. If it is true, it will return array.
You can try the code at php fiddle online, works for me
$list = '[{"productId":"epIJp9","name":"Product A","amount":"5","identifier":"242"},{"productId":"a93fHL","name":"Product B","amount":"2","identifier":"985"}]';
$decoded_list = json_decode($list);
echo count($decoded_list);
print_r($decoded_list);