John Montgomery John Montgomery - 2 months ago 12
PHP Question

How to "extract" values from Multidimensional Array

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 .


  1. [class] => span3 scrape_img the image Href & image src

  2. [class] => price scrape_price the price

  3. [class] => scrape_make the make

  4. [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
)
)
)
)
)

Answer

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']
Comments