0

我无法弄清楚如何从这个数组中获取我需要的值 ::: 我需要 3 辆车的值是 .

  1. [class] => span3 scrape_img图片 Href & 图片 src
  2. [class] => price scrape_price价格
  3. [class] => scrape_make制作
  4. [class] => scrape_year年份

我试图通过阅读PHP 数组文档来解决这个问题,但没有取得多大成功。

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

2 回答 2

1

试试这个:如果这个数组在一个名为的变量中$array

1-$array['query']['result']['div'][0]['a']['href']

2-$array['query']['result']['div'][1]['p']

3-$array['query']['result']['span'][0]['class']

要调试并找到所需的值,请尝试这样做,

var_dump($array['query']);

然后

var_dump($array['query']['result']);

...ETC

于 2013-02-04T18:14:48.013 回答
1

看起来您可以从评论和其他答案中弄清楚。但是,我应该澄清一下,我对这个问题的评论有点误导,所以你可以忽略它们。我实际上假设class引用了 PHP 对象,而实际上class只是一个字符串的键(在我看来,该数组就像一些已解析的 HTML/XML)。var_dump()当您在评论中发布数组时,我意识到了这一点。

问题是数组结构相当复杂,所有的嵌套都在进行,所以可能很难看出是什么。总的来说,我同意另一个答案;调试var_dump()是一个很好的检查方法。这是我对答案的看法。

假设您的数组变量是$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']
于 2013-02-04T19:44:40.893 回答