如何从多维数组中“提取”值

时间:2013-02-04 18:02:12

标签: php multidimensional-array

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

  1. [class] => span3 scrape_img 图片Href& image src
  2. [class] =>价格scrape_price 价格
  3. [class] => scrape_make 制作
  4. [class] => scrape_year 年度
  5. 我试图通过PHP array documentation来解决这个问题但没有取得多大成功。

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

2 个答案:

答案 0 :(得分: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']);

...等

答案 1 :(得分: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']