Need help breaking down JSON string in PHP [duplicate] - javascript

This question already has answers here:
How to convert JSON string to array
(17 answers)
Closed 7 years ago.
I need to be able to grab some data from a string using PHP.
I got an API from the games website and need to break it down.
The string I need to break down is this:
http://services.runescape.com/m=itemdb_rs/api/catalogue/detail.json?item=1513
I need to get the small icon image from that string which is the first url in the string, and the current price, which in the string is 887.
So, where it states this:
"current":{"trend":"neutral","price":887}
I need to grab the 887 and put it into a variable.
I'm using PHP,
thanks in advance if anyone can help :)

Use json_decode() for this purpose:
$item = json_decode($json)->item;
$price = $item->current->price;
$icon = $item->icon;

Download the JSON and convert to an object.
$item = json_decode(file_get_contents('http://services.runescape.com/m=itemdb_rs/api/catalogue/detail.json?item=1513'))->item;
$name = $item->name;
// etc.
Here's a function you should keep handy. It is useful for printing formatted objects and arrays, which makes it much easier to examine their structures.
function debug($v) {
echo '<pre>';
print_r($v);
echo '</pre>';
}

Related

Can I loop inside an PHP array like in JavaScript? [duplicate]

This question already has answers here:
How to determine if an array has any elements or not?
(8 answers)
Closed 4 years ago.
Is possible to loop trough an array in php like we do it in JavaScript for example without using the for ( $X as $Y){}
For example we in JavaScript we can use this code :
var names=['john','tom','jane'];
for (i=0;i<names.length;i++){
names[i];
}
Now in the case of using the same method for this loop it would be this one and it gives us an error :
$names=['john','tom','jane'];
for ($i=0;$i<$names.length;$i++){
$names[$i];
}
So is there a way around this?
You can use count() for the length of the array.
$names = ['john', 'tom', 'jane'];
for ($i=0; $i < count($names); $i++){
echo $names[$i];
}
First of all the code gives you errors because you have written lenght wrong. It should be sizeof() or count().Secondly there is also another option for looping through array. That’s foreach. As I’m not on the computer you could check out the php manual for foreach and how to use it. I hope I was useful !

I want to seperate the PHP JSON string in jQuery [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I have a problem with separating the json array result. When I alert the json result in jquery it returns me the following array.
{"productid":"17","product_quantity":"2"}{"productid":"9","product_quantity":"1"}
Now I want to separate every value in a different variable.
Thanx in advance.
that is not a valid json string.
first, you can convert the input to json string.
then, you can use JSON.parse to get the js array.
e.g.
first you need to do this:
input = '[{"productid":"17","product_quantity":"2"},{"productid":"9","product_quantity":"1"}]'
then:
input_array = JSON.parse(input)
It might be that your server returns a string that is not valid JSON. A valid example would be:
[{"productid":"17","product_quantity":"2"},{"productid":"9","product_quantity":"1"}]
How are you creating the json? Since you tagged PHP, the correct way (if you have an array) is like this, and it will return valid JSON, that you JS can handle:
echo json_encode($array);
The json you gave is misformed. I assume that's a typo.
Use JSON.parse to convert to a javascript object.
var jsonString = [{"productid":"17","product_quantity":"2"}, {"productid":"9","product_quantity":"1"}];
var data = JSON.parse(jsonString);
console.log(data[1].productid); // 9
But I do not know what you mean by."Now I want to separate every value in different variable."
Why?
Anyways you could do this. Though I do say dont. But you asked.
data.forEach(function(item){
window["productid"+item.productid] = item.product_quantity;
});
will give you 2 vars in global scope
console.log(productid17); // "2"
console.log(productid9); // "1"

Get text of curtain element ID in PHP [duplicate]

This question already has answers here:
How do you parse and process HTML/XML in PHP?
(31 answers)
Closed 7 years ago.
I need to parse the text out of an h3 element on an HTML page and save the text into a variable.
<h3 class="names-header">Names</h3>
I need the output:
Names
Saved into a variable like
$text = $output;
I've tried using DOMs, specifically this example but I've had no luck.
I've also tried to extract the data using JQuery, and submitting it as a post using Ajax on the same page. Then grabbing the post and saving it in PHP. This also didn't work, and it seems like there is a much quicker way to do this.
I've googled and tried for around 2 hours now and still can't figure out how to fix it. Any help/advice would be greatly appreciated right now. Thank you.
it would be easy to use jquery to do this! just use ajax like in the following code.
$.ajax({
type: 'POST',
url:'your php page',
data:{name: $('.names-header').text()},
success:function(response){
alert(response);
}
})
in you php do the following.
if(isset($_POST['name'])){
echo $_POST['name'];
}else{
echo 'no data to show';
}
this will allow you to catch the post data and do what ever you want.

parsing json string using json parse method [duplicate]

This question already has answers here:
How can I access and process nested objects, arrays, or JSON?
(31 answers)
Closed 8 years ago.
Hi friends I have a I have a json string as shown below. How to parse the string to get day,min_amount,max_amount values .
[{"day":"1970-01-01","min_amount":"0.00","max_amount":"0.00"},{"day":"1970-01-02","min_amount":"1.00","max_amount":"2.00"}]
Just use JSON.parse. The syntax for accessing a value is simple:
obj = JSON.parse(json)
day = obj[0].day
min_amount = obj[0].day
max_amount = obj[0].day
The great thing about Javascript is how simple it is to use JSON, because JSON is just a serialized version of plain-old javascript hashes, arrays, and scalars.
It's already in object form. SO use this :
var x = [{"day":"1970-01-01","min_amount":"0.00","max_amount":"0.00"},{"day":"1970-01-02","min_amount":"1.00","max_amount":"2.00"}]
jQuery.each(x,function(e){
console.log(x[e])
console.log(x[e].day)
});
Here is the working example : http://jsfiddle.net/u6J8A/
As you may not have noticed, JSON is JavaScript synthax.
<script type="text/javascript">
var data = [
{"day":"1970-01-01","min_amount":"0.00","max_amount":"0.00"},
{"day":"1970-01-02","min_amount":"1.00","max_amount":"2.00"}
];
</script>
Dumping it directly in the JavaScript code is perfectly valid.
But if you are fetching this data at run time and have the information as a string, you can convert it using JSON.parse(string).
The information can be then read from this structure by the variables data[0].day, data[0].min_amount, data[0].max_amount, data[1].day, data[1].min_amount, data[1].max_amount.

Something similar like <?php echo $_GET['height'];?> in HTML? [duplicate]

This question already has answers here:
How can I get query string values in JavaScript?
(73 answers)
Closed 9 years ago.
im wondering if there is a function or something like php's <?php echo $_GET['height'];?> but in HTML or javascript.
I need it to get variables of the URL
Use location.search. It returns the entire query string, which you then can split, if you need to.
Html is a markup language, so you can't do such things.
Try using javascript.
here's an example:
//returns the height of the client
function height(){
var clientHeight = document.body.clientHeight;
alert(clientHeight);
}

Categories

Resources