I'm unable to figure out how to get the values I need from this array ::: the values I need for the 3 vehicles is .
[class] => span3 scrape_img the image Href & image src
[class] => price scrape_price the price
[class] => scrape_make the make
[class] => scrape_year the year
I've tried to figure this out by going through PHP array documentation without much success.
Array
(
[query] => Array
(
[count] => 12
[created] => 2013-02-04T17:46:34Z
[lang] => en-US
[results] => Array
(
[div] => Array
(
[0] => Array
(
[class] => span3 scrape_img
[span] => Array
(
[class] => scrape
[a] => Array
(
[href] => http://cant-reveal-this.com/cars/hyundai/ix35-2-0gls-auto/
[img] => Array
(
[alt] => Featured Image
[src] => /wp-content/uploads/2013/02/01863-270x150.jpg
)
)
)
)
[1] => Array
(
[class] => price scrape_price
[p] => From R249,900
)
[2] => Array
(
[class] => span3 scrape_img
[span] => Array
(
[class] => scrape
[a] => Array
(
[href] => http://cant-reveal-this.com/cars/hyundai/ix35-2-0gls/
[img] => Array
(
[alt] => Featured Image
[src] => /wp-content/uploads/2013/02/1930-270x150.jpg
)
)
)
)
[3] => Array
(
[class] => price scrape_price
[p] => From R239,900
)
[4] => Array
(
[class] => span3 scrape_img
[span] => Array
(
[class] => scrape
[a] => Array
(
[href] => http://cant-reveal-this.com/cars/ford/everest-3-0-tdci-xlt-4/
[img] => Array
(
[alt] => Featured Image
[src] => /wp-content/uploads/2013/02/1876-270x150.jpg
)
)
)
)
[5] => Array
(
[class] => price scrape_price
[p] => From R279,900
)
)
[span] => Array
(
[0] => Array
(
[class] => scrape_make
[strong] => Make
[content] => hyundai
)
[1] => Array
(
[class] => scrape_year
[strong] => Year
[content] => 2011
)
[2] => Array
(
[class] => scrape_make
[strong] => Make
[content] => hyundai
)
[3] => Array
(
[class] => scrape_year
[strong] => Year
[content] => 2011
)
[4] => Array
(
[class] => scrape_make
[strong] => Make
[content] => ford
)
[5] => Array
(
[class] => scrape_year
[strong] => Year
[content] => 2011
)
)
)
)
)
Looks like you were able to figure it out from the comments and the other answer. However, I should clarify that my comments on the question were slightly misleading, so you can disregard them. I actually assumed class referred to PHP objects, when in fact class was simply a key for a string (the array looks to me like some parsed HTML/XML). I realized this when you posted a var_dump() of the array in the comments.
The issue is that the array structure is fairly complicated with all the nesting going on, so it may be difficult to see what's what. In general, I agree with the other answer; debugging with var_dump() is a great way to check. Here's my take on what the answer might be.
Assuming your array variable is $array:
1.
$array['query']['results']['div'][0]['span']['a']['href']
$array['query']['results']['div'][0]['span']['a']['img']['src']
2.
$array['query']['results']['div'][1]['span']['p']
3.
$array['query']['results']['span'][0]['content']
4.
$array['query']['results']['span'][1]['content']
try this:
if this array is in a variable named $array
1-$array['query']['result']['div'][0]['a']['href']
2-$array['query']['result']['div'][1]['p']
3-$array['query']['result']['span'][0]['class']
To debug and find the desired value, try to do like that,
var_dump($array['query']);
then
var_dump($array['query']['result']);
...etc
Related
I am Working with MWS for the first time and hoping to create a program that uses the ListMatchingProducts request to average out the prices of every product that matches a query.
It should be a very simple program, but I am having trouble retrieving data.
First I make the call and get amazon's xml sheet, Then I convert the xml to an array.
Print_R shows that the array looks something like this:
Array ( [ListMatchingProductsResult] => Array ( [Products] => Array ( [Product] => Array ( [0] => Array ( [Identifiers] => Array ( [MarketplaceASIN] => Array ( [MarketplaceId] => ATVPDKIKX0DER [ASIN] => 0786866020 ) ) [AttributeSets] => Array ( [ItemAttributes] => Array ( [Author] => Array ( [0] => Stephen C. Lundin [1] => Harry Paul [2] => John Christensen ) [Binding] => Hardcover [Brand] => Hyperion [Color] => White [Creator] => Ken Blanchard [Edition] => 1 [Feature] => Great product! [ItemDimensions] => Array ( [Height] => 8.25 [Length] => 5.50 [Width] => 0.00 [Weight] => 0.54 ) [IsAdultProduct] => false [Label] => Hyperion [Languages] => Array ( [Language] => Array ( [0] => Array ( [Name] => english [Type] => Published ) [1] => Array ( [Name] => english [Type] => Original Language ) [2] => Array ( [Name] => english [Type] => Unknown ) ) ) [ListPrice] => Array ( **[Amount] => 21.00** [CurrencyCode] => USD ) [Manufacturer] => Hyperion [ManufacturerMaximumAge] => 1188.0 [ManufacturerMinimumAge] => 156.0 [NumberOfItems] => 1 [NumberOfPages] => 110 [PackageDimensions] => Array ( [Height] => 0.65 [Length] => 8.60 [Width] => 5.65 [Weight] => 0.58 ) [PackageQuantity] => 1 [PartNumber] => 9780786866021 [ProductGroup] => Book [ProductTypeName] => ABIS_BOOK [PublicationDate] => 2000-03-08 [Publisher] => Hyperion [ReleaseDate] => 2000-03-08 [SmallImage] => Array ( [URL] => http://ecx.images-amazon.com/images/I/51cHo55tbOL._SL75_.jpg [Height] => 75 [Width] => 47 ) [Studio] => Hyperion [Title] => Fish: A Proven Way to Boost Morale and Improve Results ) ) [Relationships] => Array ( ) [SalesRankings] => Array ( [SalesRank] => Array ( [0] => Array ( [ProductCategoryId] => book_display_on_website [Rank] => 4629 ) [1] => Array ( [ProductCategoryId] => 1043856 [Rank] => 2 ) [2] => Array ( [ProductCategoryId] => 2635 [Rank] => 7 ) [3] => Array ( [ProductCategoryId] => 2637 [Rank] => 18 ) ) ) ) [1] ...
I am trying to access the amount part of the array, as this is the price of the object. Eventually, I will need to access the amount of each product and so a loop will likely come into play, but right now i cannot even access one products sales amount.
Here is the code I have been trying
$value = $array->ListMatchingProductsResult->Products->Product[0]->ListPrice->Amount;
print_r($value);
And it is not working. Even calling print_r on $array->ListMatchingProductsResult is not printing an array.
Any help is greatly appreciated!
Thanks,
Matt
You are almost there. Whatever method you are using to convert the XML to a PHP array however, does just that: It creates an associative array of things, not an object. That is why you cannot access it through the ->element operator, but need to use array indexes ['element']
$value = $array['ListMatchingProductsResult']['Products']['Product'][0]['ListPrice']['Amount'];
If you're ever wondering why you're not getting something back, you can successily shorten above expresion until you do. So if a print_r(...) on above expression returns nothing, just remove one square bracket at a time from that print_r until you do get something back. You then know that the last bracket you removed was the culprit.
Array (
[ListMatchingProductsResult] => Array (
[Products] => Array (
[Product] => Array (
[0] => Array (
[Identifiers] => Array (
[MarketplaceASIN] => Array (
[MarketplaceId] => ATVPDKIKX0DER
[ASIN] => 0786866020
)
)
[AttributeSets] => Array (
[ItemAttributes] => Array (
[Author] => Array (
[0] => Stephen C. Lundin
[1] => Harry Paul
[2] => John Christensen
)
[Binding] => Hardcover
[Brand] => Hyperion
[Color] => White
[Creator] => Ken Blanchard
[Edition] => 1
[Feature] => Great product!
[ItemDimensions] => Array (
[Height] => 8.25
[Length] => 5.50
[Width] => 0.00
[Weight] => 0.54
)
[IsAdultProduct] => false
[Label] => Hyperion
[Languages] => Array (
[Language] => Array (
[0] => Array (
[Name] => english
[Type] => Published
)
[1] => Array (
[Name] => english
[Type] => Original Language
)
[2] => Array (
[Name] => english
[Type] => Unknown
)
)
)
[ListPrice] => Array (
[Amount] => 21.00
[CurrencyCode] => USD
)
[Manufacturer] => Hyperion
...
BTW, using <pre>...</pre> is helpful when trying to make sense of print_r() or var_dump().
P.S. You owe me a new space key.
Here is an sdk library that uses the ListMatchingProducts of Amazon MWS. You will be able to get the info in accessing info required.
https://github.com/choomz/amazon-mws-sdk/blob/master/search/src/MarketplaceWebServiceProducts/Samples/ListMatchingProductsSample.php
To show php errors have this set in your php.in
display_errors = On
You can also give this on top of the php script
ini_set('error_reporting', E_ALL);
ini_set('display_errors', 'On');
$data = file_get_contents("https://graph.facebook.com/246179452202174/posts?access_token=TOKEN");
$data = json_decode($data, true);
print_r(array_values($data));
That's my code. It's simple. I can see the data and when I visit the link, it gives me the same info. So that works perfect. Underneath there's some outputs from the api.
Array ( [0] => Array ( [0] => Array ( [id] => 246179452202174_685970631556385 [from] => Array ( [name] => LGB eSports [category] => Sports Team [id] => 246179452202174 ) [message] => We are back! Sorry that we have been silent! But the silence is over! We will soon update you with some breaking news, stay tuned! Do not move your eyes! You do not want to miss this... [privacy] => Array ( [value] => [description] => [friends] => [allow] => [deny] => ) [type] => status [status_type] => mobile_status_update [created_time] => 2016-05-26T13:12:23+0000 [updated_time] => 2016-05-26T13:12:23+0000 [is_hidden] => [is_expired] => [likes] => Array ( [data] => Array ( [0] => Array ( [id] => 1042109789201130 [name] => Fabio Broggi ) [1] => Array ( [id] => 713442998796859 [name] => Christian Bråten ) [2] => Array ( [id] => 1801480580073327 [name] => Paulo Henrique ) [3] => Array ( [id] => 971217726332439 [name] => Benjamin Holm Davidsen ) ) [paging] => Array ( [cursors] => Array ( [before] => MTA0MjEwOTc4OTIwMTEzMAZDZD [after] => OTcxMjE3NzI2MzMyNDM5 ) ) ) )
When I try to echo $data or maybe $data[1] or something like it, it wont work. Also removing array_values gives me a output of "Array" or "ArrayArray".
So how may I print out the data from the api? It wont work, also; how may I fetch the image? I'd like to print the title of the post, and the image. As a link for a feed at my site.
More response code for link:
Array ( [data] => Array ( [0] => Array ( [id] => 246179452202174_685970631556385 [from] => Array ( [name] => LGB eSports [category] => Sports Team [id] => 246179452202174 ) [message] => We are back! Sorry that we have been silent! But the silence is over! We will soon update you with some breaking news, stay tuned! Do not move your eyes! You do not want to miss this... [privacy] => Array ( [value] => [description] => [friends] => [allow] => [deny] => ) [type] => status [status_type] => mobile_status_update [created_time] => 2016-05-26T13:12:23+0000 [updated_time] => 2016-05-26T13:12:23+0000 [is_hidden] => [is_expired] => [likes] => Array ( [data] => Array ( [0] => Array ( [id] => 1042109789201130 [name] => Fabio Broggi ) [1] => Array ( [id] => 713442998796859 [name] => Christian Bråten ) [2] => Array ( [id] => 1801480580073327 [name] => Paulo Henrique ) [3] => Array ( [id] => 971217726332439 [name] => Benjamin Holm Davidsen ) ) [paging] => Array ( [cursors] => Array ( [before] => MTA0MjEwOTc4OTIwMTEzMAZDZD [after] => OTcxMjE3NzI2MzMyNDM5 ) ) ) ) [1] => Array ( [id] => 246179452202174_680011585485623 [from] => Array ( [name] => Yoyo xno [category] => Musician/Band [id] => 281466768681242 ) [to] => Array ( [data] => Array ( [0] => Array ( [name] => LGB eSports [category] => Sports Team [id] => 246179452202174 ) ) ) [message] => GG - Spotify: https://open.spotify.com/track/7tT6XVWfjtpNF8enpFpZT4 [privacy] => Array ( [value] => [description] => [friends] => [allow] => [deny] => ) [type] => status [status_type] => wall_post [created_time] => 2016-05-12T17:33:49+0000 [updated_time] => 2016-05-12T17:33:49+0000 [is_hidden] => [is_expired] => [likes] => Array ( [data] => Array ( [0] => Array ( [id] => 196458690716329 [name] => Funny memes pics ) ) [paging] => Array ( [cursors] => Array ( [before] => MTk2NDU4NjkwNzE2MzI5 [after] => MTk2NDU4NjkwNzE2MzI5 ) ) ) ) [2] => Array ( [id] => 246179452202174_676411742512274 [from] => Array ( [name] => LGB eSports [category] => Sports Team [id] => 246179452202174 ) [message] => Breaking news! Changes in the team. We are sad to announce that Aurora will part ways with Lgb Female. We wish to thank her for the time she spent with us and we wish her all the best for the future. Here's what she says about this herself: "My time with LGB eSports has come to an end. I am extremely thankful for the opportunity to grow within this team, go to amazing places and meet amazing people. I have learnt so much from this experience and I will never forget all the adventures we've had together! I would like to use this moment of change to look back and sincerely thank all my team-mates for this learning experience, Per Lilliefelth for looking after us, our sponsor Intel for taking care of us and LGB eSports for their immense contribution to helping us grow as an all-female team in Counter-Strike. I am certain I will still remain close to the team and look forward to seeing them at many future events. As for myself I am currently a free agent and look forward to continuing my journey as a competitive CS:GO player for many more years to come. Regards, Aurora Lyngdal" [picture] => https://scontent.xx.fbcdn.net/v/t1.0-0/s130x130/13174168_676411742512274_4595019159434717947_n.jpg?oh=dad6bffaf94a2eae60c578d58f7cf8fc&oe=57E7E3F3 [link] => https://www.facebook.com/LGBeSports/photos/a.252900201530099.1073741829.246179452202174/676411742512274/?type=3 [name] => Timeline Photos [icon] => https://www.facebook.com/images/icons/photo.gif [privacy] => Array ( [value] => [description] => [friends] => [allow] => [deny] => ) [type] => photo [status_type] => added_photos [object_id] => 676411742512274 [created_time] => 2016-05-04T15:08:52+0000 [updated_time] => 2016-05-11T20:30:20+0000 [shares] => Array ( [count] => 4 ) [is_hidden] => [is_expired] => [likes] => Array ( [data] => Array ( [0] => Array ( [id] => 1756499731303402 [name] => Antony Giordans ) [1] => Array ( [id] => 196458690716329 [name] => Funny memes pics ) [2] => Array ( [id] => 10205058377778051 [name] => Charmaine Anne D. Better ) [3] => Array ( [id] => 992479757539742 [name] => Yunus Emre Çeker ) [4] => Array ( [id] => 244636029237859 [name] => Roman Hardinger ) [5] => Array ( [id] => 1134092109982746 [name] => Khoa Nguyen ) [6] => Array ( [id] => 525969817592366 [name] => Joseph Turton ) [7] => Array ( [id] => 1152533898100318 [name] => Kristian Sørensen ) [8] => Array ( [id] => 1193252910715325 [name] => Kamil Grochu Groszek ) [9] => Array ( [id] => 1158759100821275 [name] => Ace Kjirkovski ) [10] => Array ( [id] => 10153477966360064 [name]
you must echo this variable
$data[0][0]['message']
I have a multi dimensional array of items which all have a date. The array follows the format: $relatedItems[$year][$month][$day][]. Is there a way of sorting this so that it is in date order. I've tried krsort() bu i think that will just sort the year key e.g. [2014]. Sorry if this is vague I'm new to php.
Array
(
[2014] => Array
(
[01] => Array
(
[13] => Array
(
[0] => Array
(
[type] => news-blog
[title] => Jungle News
[date] => 13th January 2014
[url] => /sport/jungle-ultra/news/jungle-news/
[description] => Description goes here
)
)
[23] => Array
(
[0] => Array
(
[type] => gallery
[title] => New Gallery
[url] => /sport/jungle-ultra/galleries/new-gallery/
[images] => Array
(
[0] => Array
(
[title] => 10.jpg
[src] => /files/cache/978f98c61f63757334fbeec79fabe482_f65.jpg
)
[1] => Array
(
[title] => 9.jpg
[src] => /files/cache/1526727d35b28930b49c69b065a229de_f64.jpg
)
[2] => Array
(
[title] => 8.jpg
[src] => /files/cache/d1b67fefed5e7b7644ad9ec1fe8559ae_f63.jpg
)
[3] => Array
(
[title] => 7.jpg
[src] => /files/cache/80160e395233a15d1b1df4df67ec565d_f62.jpg
)
[4] => Array
(
[title] => 6.jpg
[src] => /files/cache/48575e2d53c487f99633a1105e7a322b_f61.jpg
)
[5] => Array
(
[title] => 4.jpg
[src] => /files/cache/9fe01089fc9656562fb7f082499439fc_f60.jpg
)
)
)
)
[27] => Array
(
[0] => Array
(
[type] => video
[title] => Test
[url] => /sport/jungle-ultra/video/test/
[thumb] => /files/5313/8996/9327/speaker-header-small.jpg
)
)
[24] => Array
(
[0] => Array
(
[type] => video
[title] => Rich reaches half way point
[url] => /sport/jungle-ultra/video/rich-reaches-half-way-point/
[thumb] => /files/7013/9039/4602/video-cover-test.jpg
)
)
[21] => Array
(
[0] => Array
(
[type] => audio
[title] => Day 1 - Update from Alexander Island
[url] => /sport/jungle-ultra/audio/day-1-update-alexander-island/
[thumb] => /files/7013/9039/4602/video-cover-test.jpg
)
)
)
)
)
You are correct in using ksort but you need to use a recursive function. Try using this:
function sort_by_date($dates){
if(is_array($dates)){ // If it is an array
ksort($dates); // Sort the array keys
foreach($dates as $date){ // Loop through the keys
sort_by_date($date); // And call this recursive function on each of the child arrays
}
}
}
Usage:
$array = array(...); // Your date array
sort_by_date($array); // New function for sorting
I downloaded the simple_html_dom.php file and uploaded it to my web server and I immediately tested it with a simple script, but it won't work, or at least I suppose it doesn't since it doesn't output anything.
Here's the script:
<?php
require('simple_html_dom.php');
// Retrieve the DOM from a given URL
$html = file_get_html('http://davidwalsh.name/');
// Find all "A" tags and print their HREFs
foreach($html->find('a') as $e) {
echo $e->innertext . '<br>';
}
?>
I got the script from this site http://davidwalsh.name/php-notifications and the same was presented on other sites so I don't understand why it won't output anything.
My guess is that the script isn't able to retrieve any data from the other website, something like the problem I came across here: Retrieving cross-domain data php.
If it were like so, could there be any way to avoid the problem?
In BIno Carlos' answer to the question I linked before he stated "it's not really a cross domain problem because you are loading the data from the server not the browser", so could there be for example a way to load the data from the browser?
So, as suggested by user868766 in his answer, i tried the two ini_set methods which didn't report any errors, so it would seem like the script works apparently just fine. When I tried the print_r() method on $html it outputted the below:
simple_html_dom Object ( [root] => simple_html_dom_node Object ( [nodetype] => 5 [tag] => root [attr] => Array ( ) [children] => Array ( ) [nodes] => Array ( ) [parent] => [_] => Array ( [0] => -1 [1] => 1 ) [dom:simple_html_dom_node:private] => simple_html_dom Object *RECURSION* ) [nodes] => Array ( [0] => simple_html_dom_node Object ( [nodetype] => 5 [tag] => root [attr] => Array ( ) [children] => Array ( ) [nodes] => Array ( ) [parent] => [_] => Array ( [0] => -1 [1] => 1 ) [dom:simple_html_dom_node:private] => simple_html_dom Object *RECURSION* ) ) [callback] => [lowercase] => 1 [pos:protected] => 0 [char:protected] => [size:protected] => 0 [cursor:protected] => 1 [parent:protected] => simple_html_dom_node Object ( [nodetype] => 5 [tag] => root [attr] => Array ( ) [children] => Array ( ) [nodes] => Array ( ) [parent] => [_] => Array ( [0] => -1 [1] => 1 ) [dom:simple_html_dom_node:private] => simple_html_dom Object *RECURSION* ) [token_blank:protected] => [token_equal:protected] => =/> [token_slash:protected] => /> [token_attr:protected] => > [self_closing_tags:protected] => Array ( [img] => 1 [br] => 1 [input] => 1 [meta] => 1 [link] => 1 [hr] => 1 [base] => 1 [embed] => 1 [spacer] => 1 ) [block_tags:protected] => Array ( [root] => 1 [body] => 1 [form] => 1 [div] => 1 [span] => 1 [table] => 1 ) [optional_closing_tags:protected] => Array ( [tr] => Array ( [tr] => 1 [td] => 1 [th] => 1 ) [th] => Array ( [th] => 1 ) [td] => Array ( [td] => 1 ) [li] => Array ( [li] => 1 ) [dt] => Array ( [dt] => 1 [dd] => 1 ) [dd] => Array ( [dd] => 1 [dt] => 1 ) [dl] => Array ( [dd] => 1 [dt] => 1 ) [p] => Array ( [p] => 1 ) [nobr] => Array ( [nobr] => 1 ) ) [doc:protected] => [noise:protected] => Array ( ) )
The code you posted is working fine for me.
Think you can try these, put below lines on top of script :
ini_set('error_reporting', E_ALL);
ini_set('display_errors', 1);
Also check print_r of $html
print_r($html);
Hope this help.
I want to transform this array:
Array
(
[0] => Array
(
[group] => site
[key] => date_format
[value] => %d %B %Y - %H:%M:%S
)
[1] => Array
(
[group] => site
[key] => description
[value] => blah
)
[2] => Array
(
[group] => site
[key] => keywords
[value] =>
)
[3] => Array
(
[group] => pages
[key] => permalink
[value] => <page>(/<subpage>)
)
[4] => Array
(
[group] => system
[key] => plugins
[value] => a:1:{i:0;s:5:"pages";}
)
[5] => Array
(
[group] => site
[key] => title
[value] => some title
)
)
... to this ...
Array
(
[system] => Array
(
[plugins] => a:1:{i:0;s:5:"pages";}
)
[site] => Array
(
[date_format] => %d %B %Y - %H:%M:%S
[description] => blah
[keywords] =>
[title] => some title
)
[pages] => Array
(
[permalink] => <page>(/<subpage>)
)
)
Is there any ways to do this without foreach and any other loop?
No you cannot.
And this micro-optimization would be pointless. Just write a simple foreach loop and stop trying to invent a square wheel.