PHP Parsing JSON data into variables - php

Alright i have been searching all over this website for some simplistic help.
A lot of the examples here are so wrapped in code it's hard to get the understanding
to implement into my own code.
I need to loop through stats on each game and display the information via variables.
Now i can do everything myself, i just can't seem to find a for each loop willing to
display my information
My current code:
$ch2 = curl_init();
// set url
curl_setopt($ch2, CURLOPT_URL, "https://na.api.pvp.net/api/lol/na/v1.3/stats/by-summoner/" . $myid . "/ranked?season=SEASON4&api_key=(REMOVED)");
//return the transfer as a string
curl_setopt($ch2, CURLOPT_RETURNTRANSFER, 1);
// $output contains the output string
$response2 = curl_exec($ch2);
// close curl resource to free up system resources
curl_close($ch2);
$obj = json_decode($response2, true);
foreach ($obj as $val) {
echo "<b>" . $val['stats']['totalDeathsPerSession'] . "</b><br>";
echo "<b>" . $val['stats']['totalDamageTaken'] . "</b><br>";
echo "<b>" . $val['stats']['totalChampionKills'] . "</b><br>";
echo "<b>" . $val['stats']['totalDeathsPerSession'] . "</b><br>";
}
Here's the JSON data
{
"modifyDate": 1402869729000,
"champions": [
{
"id": 40,
"stats": {
"totalDeathsPerSession": 5,
"totalSessionsPlayed": 1,
"totalDamageTaken": 17488,
"totalQuadraKills": 0,
"totalTripleKills": 0,
"totalMinionKills": 15,
"maxChampionsKilled": 0,
"totalDoubleKills": 0,
"totalPhysicalDamageDealt": 6183,
"totalChampionKills": 0,
"totalAssists": 12,
"mostChampionKillsPerSession": 0,
"totalDamageDealt": 21580,
"totalFirstBlood": 0,
}
},
{
"id": 42,
"stats": {
"totalDeathsPerSession": 6,
"totalSessionsPlayed": 1,
"totalDamageTaken": 10626,
"totalQuadraKills": 0,
"totalTripleKills": 0,
"totalMinionKills": 29,
"maxChampionsKilled": 1,
"totalDoubleKills": 0,
"totalPhysicalDamageDealt": 11166,
"totalChampionKills": 1,
}
],
"summonerId": 29283170
}
it keeps going on and on, i don't want to flood the entire log here.
I am simply just trying to grab some of the data like Total Minion Kills
Total Deaths, Kills.. But it keeps coming up blank with everything i try.
I have attempted so many different ways before coming here. i really hope you can help!

It should be:
foreach ($obj['champions'] as $val) {

Related

Replace String with Text

I have a website made by previous developed( Now he is not available ).
I have to access to CMS, payment backend everything, But old developer didnt left any records of database.
I am trying to rename or replace some of the text(company details) in receipt, but I dont know what am i doing wrong.
If I had an access to DB , it would be ease like butter.
Here is what I want to do :
You can see that I tried to include direct text "www.transit.com" , but it didnt show up on receipt .
Instead it showed blank.
I know its pointing to DB name like 'shop_name' and so on. But since I dont have any details on DB , I cant change it from DB.
function setShopData() {
// ショップ情報
$objDb = new SC_Helper_DB_Ex();
$arrInfo = $objDb->sfGetBasisData();
// ショップ名
$this->lfText(125, 60, $arrInfo['shop_name'], 8, 'B');
// URL
$this->lfText(125, 63, $arrInfo['www.transit.com'], 8);
// 会社名
$this->lfText(125, 68, $arrInfo['law_company'], 8);
// 郵便番号
$text = '〒 ' . $arrInfo['law_zip01'] . ' - ' . $arrInfo['law_zip02'];
$this->lfText(125, 71, $text, 8);
// 都道府県+所在地
$text = $this->arrPref[$arrInfo['law_pref']] . $arrInfo['law_addr01'];
$this->lfText(125, 74, $text, 8);
$this->lfText(125, 77, $arrInfo['law_addr02'], 8);
$text = 'TEL: '.$arrInfo['law_tel01'].'-'.$arrInfo['law_tel02'].'-'.$arrInfo['law_tel03'];
//FAX番号が存在する場合、表示する
if (strlen($arrInfo['law_fax01']) > 0) {
$text .= ' FAX: '.$arrInfo['law_fax01'].'-'.$arrInfo['law_fax02'].'-'.$arrInfo['law_fax03'];
}
$this->lfText(125, 80, $text, 8); //TEL・FAX
if (strlen($arrInfo['media-support.transit-grp.com']) > 0) {
$text = 'Email: '.'media-support.transit-grp.com';
$this->lfText(125, 83, $text, 8); //Email
}
So is there anyway I can replace these code, so that it would reflect on receipt?
Thank you
Try:
$this->lfText(125, 63, 'www.transit.com', 8);
You were referring to your array, but it didn't contain that key, you simply wanted to use a hardcoded string instead.
For the email (for instance) do:
$text = 'Email: mycompany#gmail.com';
$this->lfText(125, 83, $text, 8);
I suppose the best option is to replace the value in the array, that way any other places where this name is used will also be replaced.
function setShopData() {
// ショップ情報
// Here is the change:
$arrInfo['shop_name'] = "something else";
$objDb = new SC_Helper_DB_Ex();
$arrInfo = $objDb->sfGetBasisData();
// ショップ名
$this->lfText(125, 60, $arrInfo['shop_name'], 8, 'B');
// URL
$this->lfText(125, 63, $arrInfo['www.transit.com'], 8);
// 会社名
$this->lfText(125, 68, $arrInfo['law_company'], 8);
// 郵便番号
$text = '〒 ' . $arrInfo['law_zip01'] . ' - ' . $arrInfo['law_zip02'];
$this->lfText(125, 71, $text, 8);
// 都道府県+所在地
$text = $this->arrPref[$arrInfo['law_pref']] . $arrInfo['law_addr01'];
$this->lfText(125, 74, $text, 8);
$this->lfText(125, 77, $arrInfo['law_addr02'], 8);
$text = 'TEL: '.$arrInfo['law_tel01'].'-'.$arrInfo['law_tel02'].'-'.$arrInfo['law_tel03'];
//FAX番号が存在する場合、表示する
if (strlen($arrInfo['law_fax01']) > 0) {
$text .= ' FAX: '.$arrInfo['law_fax01'].'-'.$arrInfo['law_fax02'].'-'.$arrInfo['law_fax03'];
}
$this->lfText(125, 80, $text, 8); //TEL・FAX
if (strlen($arrInfo['law_email']) > 0) {
$text = 'Email: '.$arrInfo['law_email'];
$this->lfText(125, 83, $text, 8); //Email
}
One thing that I find odd is function setShopData() { that is usually function setShopData($arrInfo) {.
Since that is missing I assume the array is global and is set somewhere else.
At some point in the code there is a line like:
$arrInfo['shop_name'] =<database value>
If you place the line above in the code just below this any changes in the rest of the code will have the new shopname everywhere, not just on the receipt.

edit JSON Objects in JSON Objecs with PHP

How can i increment x or y in this JSON file? I posted my PHP code to increment the counter, but i dont know how to acces the "infos" -> "x" or "y".
JSON file
{
"counter": 4,
"infos": {
"x": 1,
"y": 2
}
}
PHP file
<?php
$contents = file_get_contents('../test.json');
$contentsDecoded = json_decode($contents, true);
$contentsDecoded['counter']++;
$json = json_encode($contentsDecoded);
file_put_contents('../test.json', $json);
?>
Just use
$decoded['infos']['x']++;

PHP Notice: Array to string conversion when echoing json_decode()'ed data

I have this code in a.json:
{
"body" : {
"outboundSMSMessageRequest": {
"address": [
"9456654978" /* 1 */
],
"senderAddress": " 64735 ", /* 3 */
"outboundSMSTextMessage": {
"message": "Welcome to fgf Your Confirmation Code - " /* 2 */
},
"clientCorrelator": "1", /* 4 */
"receiptRequest": {
"notifyURL": "2", /* 5 */
"callbackData": "3" /* 6 */
},
"senderName": "4" /* 7 */
}
}
}
I want to print the values as marked above (1, 3, 2, 4, 5, 6, 7, the values only). I have tried this:
$jsonData = file_get_contents("a.json");
$json = json_decode($jsonData,true);
echo $json;
but I end up with the following notice:
PHP Notice: Array to string conversion
How can I print the values as desired?
Presumably by:
I want to in a PHP page to print (1, 3, 2, 4, 5, 6, 7, the values only)
... you mean those values you've marked with the numbered comments, e.g. /* 1 */ - if so, this should do the trick (to just echo it out anyway).
$jsonData = file_get_contents("a.json");
$o = json_decode($jsonData);
$outboundSMSRequest = $o->body->outboundSMSMessageRequest;
echo $outboundSMSRequest->address[0] . "\n" // outbound address
. $outboundSMSRequest->outboundSMSTextMessage->message . "\n" // outbound message
. $outboundSMSRequest->senderAddress . "\n" // sender address
. $outboundSMSRequest->clientCorrelator . "\n" // client correlator
. $outboundSMSRequest->receiptRequest->notifyURL . "\n" // notification URL
. $outboundSMSRequest->receiptRequest->callbackData . "\n" // callback data
. $outboundSMSRequest->senderName; // sender name
When you use json_decode() you turn the JavaScript object into a PHP object - so you just access it accordingly.
Json do not support comments so you need to remove them from your file,
see Can comments be used in JSON?

Fpdf and PDF_LABEL

I am trying to set up a labeling printing system in PHP and on each label I would like to add a logo on top of it and I can't manage to work it, because on fpdf images are treated as a cell it doesn't work
EDIT:
The code is as follows:
for($i=1;$i<=$nolabels;$i++) {
$text = sprintf("%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s%s%s", "", "","","", "$destination", "$label", "USE BY: $date", "$i", '/', "$nolabels");
$pdf->Add_Label($text);}
for($i=1;$i<=$nolabels2;$i++) {
$text = sprintf("%s\n%s\n%s\n%s%s%s", "$destination", "$label2", "USE BY: $date", "$i", '/', "$nolabels2");
$pdf->Add_Label($text);
You can make additional "addHeader" and call it on each page. Here is one example with using X and Y coordinates
public function addHeader($pdf, $addPageNum)
{
// Header
if ($addPageNum) {
$pdf->SetFont('Arial', '', 10);
$pdf->SetTextColor(168, 168, 168);
$pdf->SetXY(20, 265);
$pdf->Cell(0, 5, __('Page') . ' ' . $pdf->PageNo() . ' von ' . '{nb}', 0, 1, 'C', 0);
}
// Footer photo
$pdf->Image(ROOT . '/webroot/img/companyLogo.png', 0, 270, 210);
}

PHP Cross Session Global Counter Variable (Shared Memory)

On a very busy PHP server, I am trying to get a global counter going and thought shared memory would be the way to go. Ideally I would use APC
apc_add('counter',1);
apc_inc('counter',1);
var_dump(apc_fetch('counter'));
except that it is not available on the servers and I am restricted to what is available. The servers do have shmop so tried to get that to work, however it seems to be session specific. Is there anything else that could be used?
The shmop code I tried:
$shm_key = ftok(__FILE__, 't');
$shm_id = shmop_open($shm_key, "c", 0644, 8);
$shm_data = shmop_read($shm_id, 0, 8);
$shm_data = shmop_read($shm_id, 0, 8);
var_dump($shm_data);
if (empty($shm_data)) {
//counter has not been set
$shm_bytes_written = shmop_write($shm_id, 0, 1);
} else {
$shm_bytes_written = shmop_write($shm_id, (int)$shm_data + 1, 0);
}
$shm_data = shmop_read($shm_id, 0, 8);
var_dump($shm_data);

Categories