Issues with decoding JSON data in PHP

Struggling to assign a JSON Object to a PHP Variable? When attempting to use var_dump, it's showing NULL.

Here is the PHP code:

<?php
$json   = utf8_encode(file_get_contents('http://socialclub.rockstargames.com/ajax/stat/1/profile_body/sta1/drantifat'));
$data   = json_decode($json, false);

$money = $data->tplc->stats;   // -> How can I access "GTA Online Cash" with id: and value: $33.9K ?
?>

Sample of the JSON file: (it may change)

{"cid":0,"ttl":"Grand Theft Auto V",
"ishs":false,"issa":false,"istc":false,"htl":true,"tlc":"text",
"tlt":"View Stats","tlh":"/member/drantifat/games/gtav/career/overview",
"iso":false,"cmng":false,"order":0,"uid":0,"ttc":0,
"tplc":"{\"game\":\"GTAV\",\"stats\":[{\"id\":\"Game Progress\",\"val\":\"58.77%\"},
{\"id\":\"Missions Passed\",\"val\":\"55\"},{\"id\":\"Playing Time\",\"val\":\"29:00:33\"},
{\"id\":\"GTA Online RP\",\"val\":\"2.4M\"},{\"id\":\"GTA Online Rank\",\"val\":\"128\"},
{\"id\":\"GTA Online Cash\",\"val\":\"$33.9K\"},
{\"id\":\"GTA Online Playing Time\",\"val\":\"529:44:12\"}],
\"url\":\"/member/drantifat/games/gtav/career/overview\"}","isa":false,"cnt":""}

If you have any insights on how to achieve what the PHP file mentions, your input would be greatly appreciated.

Answer №1

tplc isn't just a regular JSON object, it's actually a string that is formatted as JSON itself.

<?php
$json = utf8_encode(file_get_contents('http://socialclub.rockstargames.com/ajax/stat/1/profile_body/sta1/drantifat'));
$data = json_decode($json, false);
$tplc = json_decode($data->tplc);
$money = $tplc->stats;
?>

If you're looking to extract the money value specifically:

<?php
$json = utf8_encode(file_get_contents('http://socialclub.rockstargames.com/ajax/stat/1/profile_body/sta1/drantifat'));
$data = json_decode($json, false);
$tplc = json_decode($data->tplc);
$stats = $tplc->stats;
foreach ($tplc->stats as $stat) {
    if ($stat->id == 'GTA Online Cash') {
        $money = $stat->val;
        break;
    }
}
echo $money;
?>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What is the best way to interpret this JSON information?

Could someone provide guidance on how to properly parse the JSON response below? Here is my function: function retrieveSupplierData() { $.getJSON('accountpayble_getDataOnSuplierForInvoiceAdd.action', function(data) { ...

What is the best way to handle mapping an array with uncertain levels of nesting?

My task involves rendering an array of comments in ReactJs, each of which can have nested comments at unknown levels. I am struggling to figure out how to display these comments with their respective nesting levels. comment 1 -- comment 2 -- comment 3 --- ...

What is the best way to compare two unique strings that both point to the same directory?

Consider the following scenario: although $dir1 and $dir2 point to the same directory, their string values are different. How can I determine if they are referring to the same directory? $dir1 = 'application/somedir/some_subdir/../'; $dir2 = ...

Creating a CodeIgniter session that expires when the user closes their browser

In my CodeIgniter session configuration, I have disabled the "destroy_on_close" feature. Although I don't usually need it, there is one instance where I do. I have added a "remember me" checkbox for user login purposes. Typically, when a user logs ...

Two database queries housed within a single array

I am currently working with 2 queries that are extracting elements from 2 separate databases. My goal is to combine the results of both queries into a single array. Right now, I have one array return, but I want to merge the data from these 2 queries into ...

marshalling and unmarshalling data in a straightforward manner

One of the exciting new features in Delphi 2009 and Delphi 2010 is the introduction of JSON support. I am curious to find out if there is a straightforward function available for marshalling/unmarshalling directly between strings and objects, similar to wh ...

Guide to crafting a javascript variable from MySQL through the power of PHP and AJAX

I am not very experienced in working with AJAX and javascript. I am currently trying to pass longitude and latitude values from a MySQL database to javascript, but it doesn't seem to be working as expected. Can anyone help me figure out what I might b ...

Generate a fresh array using the data from the existing array object

I have nested objects within arrays that I need to manipulate. { "page": [ { "num": "1", "comp": [ { "foo": "bar", "bar": "foo", ...

Utilize preg_replace to insert your own website ahead of each hyperlink

In need of a solution for my project which involves fetching website content and modifying the HTML code. All links on the website must be replaced with my own links, but I encountered an issue with classes being assigned to some links. Initially, I tried ...

function for extracting data from MySQL JSON documents

Within my MySQL database, I have a column called json: {"myarray":[ { "users":"82,191", type":"3" } ]} The data in the above table shows that the column json contains information about users. Specifically, I am ...

Order by two different meta attributes

I'm having trouble sorting data using two meta keys simultaneously. The following code is not yielding the desired results: $args= array( "post_type" => "post_type", "post_status" => "publish", 'meta_query' => array( ...

Crafting 3 intertwined combinations using the power of jQuery AJAX and PHP

Here's what I've been working on so far: The first page retrieves data and populates the first combobox, which is all good. Then, when users select a value from combo1, a second combobox is created with filtered data using AJAX - also working fin ...

Is there a way to work around a failed email while sending several emails using swiftmailer?

I am facing an issue with processing unsent emails from a MySQL database using cron. The problem arises when one email fails to send, causing the entire process to stop. This happened due to an SMTP authentication failure, and I need to find out if there a ...

I am encountering an error when trying to fetch a JSON response from a PHP script, even though I am able

Below is the Javascript code I am using to initiate an AJAX call from a PHP file: $(document).ready(function(e) { $(function(){ $.ajax({ type:'GET', dataType: 'jsonp', data: { ...

Ways to retrieve all categories, including their children, as objects in Wordpress

Is there a method in Wordpress to retrieve categories and child categories as a PHP object without the HTML elements provided by wp_dropdown_categories()? Or do I need to create my own function to recursively fetch the categories? I also need to customize ...

PHP Notice: Undefined index: 0

I'm encountering an issue with this specific section "; $primary_image = dbSelect($db, $query); $project->primary_image = $primary_image[0]; } within this piece of code <?php require_once('includes/config.php'); ...

Convert a String to JSON using either JavaScript or jQuery

Currently, I am developing a JavaScript animation script and I am aiming to allow individuals to define behavior declaratively within the HTML. This is the format I envision for my HTML: <div data-animation="top: 600, left: 600, opacity: 1, start: 0.2 ...

Error message encountered: "When fetching JSON data in React Native, the error 'undefined

== UPDATE == I'm facing an issue with fetching a JSON data. I am trying to retrieve a JSON from Google Maps, but it is returning as undefined. Here is the code snippet: const [isLoading, setLoading] = useState(true); const [info, setInfo] = u ...

Can we create a class to represent a JSON object?

Can a JSON be modeled with a class in TypeScript (or Angular)? For example, I am using Firebase and have a node called /books structured like this: books -- 157sq561sqs1 -- author: 'Foo' -- title: 'Hello world' (Where 1 ...

I seem to be stuck trying to figure out what's going wrong when receiving JSON data

I have spent hours researching on Stack Exchange and Google to gather different information. Here is what I have in guess.php: PHP header('Content-type: application/json'); get_rating($cleanMovie); json_encode(array($id)); The get_rating funct ...