Json data group by team name with php - php

I have to display team points under respective team name
I have following json data
{
"id":319231,
"innings":[
{"id":967766},
{"id":967767},
{"id":967768},
{"id":967769}
],
"team1":{
"team":{"name":"Minor Counties","id":115104,"club":{"name":"Minor Counties","id":98110}},
"innings":[
{"id":967766,"points":253,"wickets":10,"overs":86,"balls":4},
{"id":967768,"points":190,"wickets":5,"overs":61,"balls":0}
]
},
"team2":{
"team":{"name":"Major Counties","id":93648,"club":{"name":"Major Counties","id":35487}},
"innings":[
{"id":967767,"points":229,"wickets":10,"overs":67,"balls":4},
{"id":967769,"points":64,"wickets":4,"overs":23,"balls":2}
]
},
}
Now I want result like :
Minor Counties Major Counties
253/10 & 190/5 229/10 & 64/4
Currently I am getting result like :
Minor Counties Minor Counties Major Counties Major Counties
253/10 190/5 229/10 64/4
Here is my php code so far :
$team1 = $read_json->team->team1->name;
$team2 = $read_json->team->team2->name;
foreach($read_json->team1->innings as $team1Innings){
$points = $team1Innings->points;
$wickets = $team1Innings->wickets;
$overs = $team1Innings->overs;
$balls = $team1Innings->balls;
echo "<div class=\"score-total\"><span class=\"score-team\">$team1</span>$points/$wickets<span class=\"score-overs\">$overs.$balls overs</span></div>";
}
similar code to get team2 points

$json = '{
"id":319231,
"innings":[
{
"id":967766
},
{
"id":967767
},
{
"id":967768
},
{
"id":967769
}
],
"team1":{
"team":{
"name":"Minor Counties",
"id":115104,
"club":{
"name":"Minor Counties",
"id":98110
}
},
"innings":[
{
"id":967766,
"points":253,
"wickets":10,
"overs":86,
"balls":4
},
{
"id":967768,
"points":190,
"wickets":5,
"overs":61,
"balls":0
}
]
},
"team2":{
"team":{
"name":"Major Counties",
"id":93648,
"club":{
"name":"Major Counties",
"id":35487
}
},
"innings":[
{
"id":967767,
"points":229,
"wickets":10,
"overs":67,
"balls":4
},
{
"id":967769,
"points":64,
"wickets":4,
"overs":23,
"balls":2
}
]
}
}';
$items = json_decode($json);
unset($items->id);
unset($items->innings);
foreach ($items as $item) {
echo "<b>{$item->team->name}</b>";
$innings = [];
foreach ($item->innings as $inning) {
$innings[] = "{$inning->points} / {$inning->wickets}";
}
echo '<br>';
echo implode(' & ', $innings);
echo '<br>';
}

Use json_decode() with true as second parameter it gives you an associative array and it will convert the JSON object into a PHP object.It will make your life easier.
Try this :
$jsonObj = json_decode($json,true);
$inningsPoint = [];
foreach ($jsonObj as $teamData) {
if(!empty($teamData[team][name])) {
echo $teamData[team][name].'<br>';
$inningsPoint = [];
}
foreach ($teamData[innings] as $inningsData) {
$inningsPoint[] = "$inningsData[points] / $inningsData[wickets]";
}
echo implode(' & ', $inningsPoint);
}
Demo!

<?php
function showteams_results($k1,$points){
switch ($k1) {
case 'team1':
foreach ($points as $key => $value) {
if($key=="team1")
{
echo ucfirst($key)."<br>";
foreach ($value as $k => $v) {
echo "Innings".$v."<br>";
}
}
}
break;
case 'team2':
foreach ($points as $key => $value) {
if($key=="team2")
{
echo ucfirst($key)."<br>";
foreach ($value as $k => $v) {
echo "Innings".$v."<br>";
}
}
}
break;
default:
echo "Team no points!";
break;
}
}
$points=array();
$jsonObj = json_decode($json,true);
$inningsPoint = [];
foreach ($jsonObj as $k1 => $teamData) {
//extracting points of team1---------->
if($k1=="team1"){
//showteams_results($k,$jsonObj);
foreach ($teamData as $k => $v) {
if($k=="innings")
{
foreach ($v as $k => $vf) {
foreach ($vf as $k => $vk) {
if($k=="points")
$points["team1"][]=$vk;
}
}
}
}
//sending data teams
showteams_results($k1,$points);
}
//extracting points of team2---------->
if($k1=="team2"){
//showteams_results($k,$jsonObj);
foreach ($teamData as $k => $v) {
if($k=="innings")
{
foreach ($v as $k => $vf) {
foreach ($vf as $k => $vk) {
if($k=="points")
$points["team2"][]=$vk;
}
}
}
}
//sending data teams
showteams_results($k1,$points);
}
}
?>

Related

How to get nth element of a multidimensional array in PHP?

I want to select the "RSI" from the first element only.
Array (json file):
{
"Technical": {
"2019-01-11 15:30": {
"RSI": "123"
},
"2019-01-11 14:30": {
"RSI": "456"
}
"2019-01-11 14:30": {
"RSI": "789"
}
}
My php:
foreach ($json['Technical'] as $field => $value) {
echo $value['RSI']; // Gives 123456789
}
I want only 123
I tried:
echo $value[0]['RSI']; // Gives NULL
Break the loop with break; and it will only return the first item.
foreach ($json['Technical'] as $field => $value) {
echo $value['RSI']; // Gives 123
break;
}
If you want specific items then use a "$key" variable.
$key = 0;
foreach ($json['Technical'] as $field => $value) {
if($key == 0 || $key ==1){
echo $value['RSI'];
}
$key++;
}
// 123456
Change the if to suit your needs.

Fetching data from json array and comparing

Hello i have a json array which contains an array structure. I am trying to fetch the value against a particular key. Like getting a key with name lower and value 9226. I have implemented for each loop but i cannot get into it. There is some issue in my comparison statement.
{
"tax_structure": [
{
"tax_data": {
"lower": 0,
"upper": 9225,
"tax_rate": 10,
"amount": 0
}
},
{
"tax_data": {
"lower": 9226,
"upper": 37450,
"tax_rate": 15,
"amount": 922.50
}
}
]
}
Php file:
<?php
$expense=10000;
$str_data = file_get_contents("data.json");
$data = json_decode($str_data,true);
foreach ($data["tax_structure"] as $tax_data) {
foreach ($tax_data["tax_data"] as $key => $value) {
if($key=="lower" && $expense>$value) & ($key=="upper" &&$expense<$value)
{
//do something if expenses value falls between the lower and upper limit values of keys
}
}
}
?>
I believe you are looking for something like the following.
There is no need for the second for loop.
$expense = 10000;
$str_data = file_get_contents("data.json");
$data = json_decode($str_data,true);
foreach ($data["tax_structure"] as $tax_data_object) {
if($tax_data_object['tax_data']['lower'] < $expense && $tax_data_object['tax_data']['upper'] > $expense){
// Do stuff
}
}
EDIT
You should try print_r($data);. This will show the structure of your data array.
If you examine the structure of your data the above code should be obvious.
If it isn't, here are a few sources for further learning:
PHP: Arrays
JSON introduction
<?php
$expense = 10000;
$str_data = file_get_contents("data.json");
$data = json_decode($str_data, true);
foreach ($data["tax_structure"] as $tax_data) {
foreach ($tax_data["tax_data"] as $key => $value) {
if ($key == "lower" && $expense > $value) {
echo "Key is lower and the value is " . $value . "<br>";
}
if ($key == "upper" && $expense < $value) {
echo "Key is upper and the value is " . $value . "<br>";
}
}
}
?>
You cant do this if($key=="lower" && $expense>$value) & ($key=="upper" &&$expense<$value)

json decode array PHP

I'm trying to decode a json string, i want to get just langlinks value,
my json string is:
{
"batchcomplete": "",
"query": {
"pages": {
"105219": {
"pageid": 105219,
"ns": 0,
"title": "Cancer",
"langlinks": [
{
"lang": "ar",
"*": "\u0633\u0631\u0637\u0627\u0646"
}
]
}
}
}
}
I tried this code:
$results = json_decode($api_response, true);
$list = array();
foreach ($results['query']['pages'] as $k => $v)
{
var_dump($v);
foreach($v as $key => $val)
{
array_push($list, $val);
}
}
return $list;
But it does not accede to the value that I want, when i add
var_dump(array_key_exists('langlinks', $v));
it gives me false :/
I just tested this and this seems to return true
$source = file_get_contents('https://en.wikipedia.org/w/api.php?action=query&titles=Cancer&prop=langlinks&lllang=ar&format=json');
$results = json_decode($source, true);
foreach ($results['query']['pages'] as $k => $v)
{
var_dump(array_key_exists('langlinks', $v));die();
}
If you are still having trouble, maybe you can post more code?
$list = array();
$src = json_decode($api_response, true);
foreach ($src['query']['pages'] as $key => $langData) {
foreach ($langData['langlinks'] as $k => $ld);
// var_dump($ld['*']);
}
return $ld['*'];

Handeling with nested multiple array in PHP (foreach)

I am trying to handle a situation with a nested multiple array that is received in PHP by $_POST from Javascript-Jquery (as Object not as Json)
. The nested Object looks like this:
{
"Videotheck":{
{
"Category":"Comedy",
"Title_Liste":[
{
"Title":"Millers",
"Year":"2014"
},
{
"Title":"Yogi",
"Year":"2012"
}
]
},
{
"Category":"Action",
"Title_Liste":[
{
"Title":"Rodulf",
"Year":"2014"
},
{
"Title":"Matrix",
"Year":"2000"
}
]
}
}
}
And now the information in this Object need to be splited. For example the title list of each category should be stored in a var
$comedy_title_liste = [];
$action_title_liste = [];
I tryed this:
if($_POST){
$arr1 = $_POST['Videotheck'];
foreach($arr1 as $vtk){
foreach($vtk as $data => $v){
foreach($v as $key => $value){
foreach($value as $k => $info){
echo $k.' '. $info;
}
}
}
}
}
Like this I can get only all title list from all categories, but is necessary to get for each category the list of titles separeted. I don't know really how to handle the situation.
Well this is what I have. I guest that there is something not correct.
Not 100% exact but you can give a try :
$result = array();
$parent = $_POST['Videotheck'];
foreach($parent as $key=> $child) {
$result[$child['Category']."_title_liste"] = array();
foreach($child['Title_Liste'] as $cKey => $val) {
$result[$child['Category']."_title_liste"][] = $val['Title'];
}
}

need to read (echo) json by php

i need to get json code that changed remotely and display the data in php
when user load the page. (echo it)
i have this code:
(JSON output:)
{
"area 267":[
"city1",
"city2",
"city3",
"city4",
"city5",
"city6",
"city7",
"city8",
"city9",
"city10",
"city11",
"city12",
"city13"
],
"area 268":[
"city14",
"city15",
"city16"
],
"Coords":[
"31.856128;34.760947",
"31.8166376;34.729755",
"31.857223;34.727432",
"31.843444;34.751793",
"31.831039;34.722577",
"31.831504;34.756643",
"31.817725;34.7223",
"31.815936;34.752588",
"31.819054;34.739162",
"31.824913;34.747459",
";",
"31.813987;34.719103",
"31.833778;34.74009",
"31.815936;34.752589",
"31.819054;34.739163",
"31.824913;34.747458",
],
"ID":"1405796061262"
}
the coords lines is the sum of the all cities.
i need to echo the json like this:
(PHP output need to be like:)
ID: 1405796061262.
Areas: area267, area268.
Citys of Area267: city1, city2, ... city13. (echo all cities in the area)
Citys of Area268: city14, city15, city16. (echo all cities in the area)
i cant know the areas and the cities (that in the JSON file).
Thanks for helping :)
EDIT: WORKING CODE
<?php
$json_data = '{
"area 267":[
"city1",
"city2",
"city3",
"city4",
"city5",
"city6",
"city7",
"city8",
"city9",
"city10",
"city11",
"city12",
"city13"
],
"area 268":[
"city14",
"city15",
"city16"
],
"ID":"1405796061262"
}';
$data = json_decode($json_data);
//var_dump($data);
echo 'ID: ';
echo $data->ID;
echo '.<br>';
echo 'Areas: ';
$areas = array();
foreach ($data as $k => $v) {
if (strpos($k, 'ID') !== 0 && strpos($k, 'Coords') !== 0) {
$areas[] = $k;
}
}
echo implode(', ',$areas).'.<br /><br />';
foreach ($areas as $k => $v) {
echo "Cities of ".$v.': ';
$cities = array();
$cities = null;
foreach ($data->$v as $area) {
$cities[] = $area;
}
echo implode(', ',$cities).'.<br />';
}
?>
first you can use json_decode to convert the json output into php object then you can easily loop it and get the exact field value.
here is the documentation link:- http://php.net/manual/en/function.json-decode.php
Probably something like this should work
$data = json_decode($json_data);
echo 'ID '.$data['ID']."<br />";
echo 'Areas: ';
$areas = array();
foreach ($data as $k => $v) {
if (strpos($k, 'area ') === 0) {
$areas[] = $k;
}
}
echo implode(',',$areas).'.<br /><br />';
foreach ($areas as $area) {
echo "Cities of ".strtoupper($area).':';
$cities = array();
foreach ($data[$area] as $city) {
$cities[] = $city;
}
echo implode(','.$cities).'.<br />';
}

Categories