Access property of a PHP object [closed] - php

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I'm passing in an object using an ajax function. The object looks like this:
{"label":"1","number":2}
Once the object reaches the server I'm using PHP to json_decode it.
After it's decoded how can I start accessing the properties of the object? For example I want to retrieve the value of label - how can this be done?

$myobj = json_decode($JSON);
print $myojb->label;
You might want to read about Classes and Objects in the manual. An alternative syntax would be decoding the JSON to an array and then accessing it via the key.
$myarr = json_decode($JSON, TRUE);
print $myarr['label'];

You can access it like this;
$json = json_decode($input);
echo $json->label;

$input=json_decode(your json);
echo $input->label;

you can do it this way....
from jquery..
var a= {'label':'1','number':'2'};
$.ajax({
data : {data:JSON.stringify(a)},
})
from php side
if(isset($_POST['data']))
{
$data = $_POST['data'];
$data = json_decode($data,true);
echo $data['label']; //will print label here
}
this is full example of ajax request with json to php...
might help you...

Related

How to print a String from a json file in PHP [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
I've a problem.
I want to print out a string the string is in this JSON file with a php scipt:
http://api.worldoftanks.eu/2.0/clan/info/?application_id=d0a293dc77667c9328783d489c8cef73&clan_id=500030916&mt_order_by=-role_hierarchy
it's: data / description
i hope you understand what i mean.
Use jsondecode php function.
$url = 'http://api.worldoftanks.eu/2.0/clan/info/?application_id=d0a293dc77667c9328783d489c8cef73&clan_id=500030916&mt_order_by=-role_hierarchy';
$jsondata = file_get_contents($url); //json data from url
$data = json_decode($jsondata,1); // convert json data to array
echo $data['data']['description']; // access data using keys of array, mapped to properties in json
using json_decode
$json_uri = 'http://api.worldoftanks.eu/2.0/clan/info/?application_id=d0a293dc77667c9328783d489c8cef73&clan_id=500030916&mt_order_by=-role_hierarchy';
$json = file_get_contents($json_uri);
$data = json_decode($json,1); // convert json data to array
$count = 1;
foreach ($data['data'] as $key => $info) {
echo $count++.' - '.$info['description'].'<br>';
}

How to grab data from an external URL [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have an application where a url will be generated from website #1, after a user signs up.
(i.e. user will get their own url: http://www.example.com/?affid=12345)
I need to grab the numbers in the above url by using PHP, but I don't understand how this would work. How can I grab the actual numbers from the url above using PHP?
I should also mention that the website where I will generating PHP code is not on the same server as the URL above.
Use parse_url and parse_str for that:
$url = 'http://www.example.com/?affid=12345';
$query = parse_url($url, PHP_URL_QUERY);
parse_str($query, $vars);
echo $vars['affid']; // outputs 12345
To get the number i.e 12345 in this URL http://www.example.com/?affid=12345, use $_GET or $_REQUEST which will give you an associative array. you can use $_GET and $_REQUEST throught the application, while using $_GET OR $_REQUEST you must clean before using in your code.
echo $_GET['affid'];
OR
echo $_REQUEST['affid'];
You can use parse_url function to extract the query
<?php
$url = 'http://www.example.com/?affid=12345';
$parsed_url = parse_url($url);
echo $parsed_url['query'];
//print : affid=12345
?>
After that, you can parse the result with parse_str function
<?php
parse_str($parsed_url['query'],$result);
var_dump($result);
//print array(1) { ["affid"]=> string(5) "12345" }
echo $values[0]
//print 12345
?>

How to convert te JSON string to a JSON object in php? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I got a object like this:
{"1":"FFS","2":"S"}, and I use json_decode function to convert it into an array, but I am not success with it. It becomes the array like this: {"FFS", "S"}, but missing the {"1", "2"}, Can I convert it to become a dict or something, that I can access both value? Thanks.
use true param, to convert object to associative array for json_decode(), like do:
$str = '{"1":"FFS","2":"S"}';
echo "<pre>"; print_r(json_decode($str, true));
gives::
Array
(
[1] => FFS
[2] => S
)
$myjsonobject = json_decode('{"1":"FFS","2":"S"}');
Should working.
Try: print_r($myjsonobject); to validate.
Its working.

php array to javascript array format [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
php array:
Array([0] => http://XXX/xgsyw/content/Uploads/Img/1111.jpg[1] => http://XXX/xgsyw/content/Uploads/Img/222.jpg)
to javascript "images"
$('#top').bgStretcher({
images: ['images/sample-1.jpg', 'images/sample-2.jpg', 'images/sample-3.jpg', 'images/sample-4.jpg', 'images/sample-5.jpg', 'images/sample-6.jpg'],
slideDirection: 'N',
slideShowSpeed: 1000,
transitionEffect: 'fade',
sequenceMode: 'normal',
});
Encode it with the JSON library. Send it back to the Javascript, decode it, and append new img child nodes to some container.
echo json_encode(arrayOfImages);
Then in your JS:
var images = JSON.decode(returnValue);
images.each(function(path) {
var img = $('<img>');
img.attr('src', returnValue);
img.appendTo('#imagediv');
});
As noted in the comments..
It is quicker to use $(document.createElement("img")); than to use the aforementioned suggestion.
You might have done some more research online, because what you need for your solution is the JavaScript Object Notation short JSON.
Use json_encode($your_array);
http://php.net/manual/de/function.json-encode.php

Parsing XML from a Web Page with PHP [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I heard that you can parse information from a web page with SimpleXML.
How can I get the the Usernames and ID's from the following page and display them on my website like:
Username:Napolien
Id:2202591
The URL to the website from which I want to get the information is:
http://api.roblox.com/users/3/friends
Your api returns json, you can decode it with json_decode, i have also include how to traverse through the json objects included in the feed.
$json = file_get_contents("http://api.roblox.com/users/3/friends");
$obj = json_decode($json);
for ($i=0; $i < count($obj); $i++) {
echo $obj[$i]->Id;
echo $obj[$i]->Username;
}
The data is actually JSON. You can get it like this :
$jsonString = file_get_content('http://api.roblox.com/users/3/friends');
$data = json_decode($jsonString, true);
$username = $data[14]['username']; // 14 is your desired index
$id = $data[14]['id'];

Categories