Getting value from JSON [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 am struggling to get pull the correct values
I am trying to get the following
packages>items then loop through the ids
{"id":1,"title":"Barnsley","slug":"barnsley","packages":[{"id":1,"title":"Group PACKAGE 1","items"
{"id":9,"title":"Guiness","band_a":null,"band_b":null,"band_c":null,"band_d":null,"band_e":null,"band_f":null,"band_g":null,"band_h":null,"band_i":null,"band_j":null,"variations":null},
{"id":10,"title":"John Smith","band_a":null,"band_b":null,"band_c":null,"band_d":null,"band_e":null,"band_f":null,"band_g":null,"band_h":null,"band_i":null,"band_j":null,"variations":null},
{"id":22,"title":"Chicken Cheese and Bacon Melt","band_a":4.5,"band_b":4.75,"band_c":5.0,"band_d":5.25,"band_e":null,"band_f":null,"band_g":null,"band_h":null,"band_i":null,"band_j":null,"variations":null}],"additional_items":null,"event_types":null}]}

Your JSON isn't valid. You're missing two characters :[ that declare the items array. After fixing the JSON (check it on jsonlint.com) you can iterate over the item IDs like so:
$obj = json_decode($str);
foreach($obj->packages[0]->items as $item)
{
echo $item->id;
}
If there could be more than one package you can iterate those too:
foreach($obj->packages as $package)
{
foreach($package->items as $item)
{
echo $item->id;
}
}

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>';
}

preg match for known value plus 7 charecters [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
Can anyone help me get the value of IFC/USD 0.00001031 from the webpage http://infinitecoin.com.
I need to grab the value after IFC/USD..
The way I thought to do it was to:
$file_string = file_get_contents('http://infinitecoin.com');
preg_match('/IFC/USD plus next 11 charecters', $file_string, $title);
$value = $title[1];
echo $title_out ;
But im not sure how to ask PHP to find IFC/USD then return the next 11 characters after that.
If I could accomplish this, my task would be solved..
Any help would be great.
Thank you
Jason
$output = array();
preg_match("/(?<=IFC\/USD\s)[\d\.]+/", 'IFC/USD 0.00001015', $output);
$output will look like this:
Array
(
[0] => 0.00001015
)

i need Apostrophe in a variable result MYSQL [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 receive data in this format like:
C00001,C00302,C00303,C00287,C00286,C00285,C00017
in a variable but i need in this shape:
'C00001','C00302','C00303','C00287','C00286','C00285','C00017'
i am new in mysql kindly any help
You can explode the data, modify it, then re-implode it.
$data = 'C00001,C00302,C00303,C00287,C00286,C00285,C00017';
$arr = explode(',', $data);
$new_arr = array_map(function($a){
return "'{$a}'";
}, $arr);
$new_data = implode(',', $new_arr);

Filtering an array of objects based on a condition [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 8 years ago.
Improve this question
I have an array in the following format :-
[
{ name : "Foo",
type : "Bar"
},
{
name : "Foo",
type : "Row"
},
{
name : "Foo"
}
]
I would like to remove occurrences of "Foo" only when it doesn't have a type. Basically, there can be duplicates in the array as long as the type is different and there can not be a duplicate with no type. Any help is appreciated!
Thanks!
I am assuming that you are working with a json_decoded array of objects. IN that case, you could run a simple array_filter() like this:
$filtered_array = array_filter($array, function($item) {
return isset($item->type);
});

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