json解码获取特定值的数据

时间:2011-10-31 17:26:37

标签: php multidimensional-array json

我正在使用json decode来获取一些数组。

返回的对象看起来像这样

   Array
   (
[0] => stdClass Object
    (
        [d] => Sun
        [eid] => 2011103005
        [ga] => 
        [gsis] => 55267
        [gt] => REG
        [h] => BAL
        [hnn] => ravens
        [hs] => 30
        [q] => F
        [rz] => 0
        [t] => 1:00
        [v] => ARI
        [vnn] => cardinals
        [vs] => 27
    )

[1] => stdClass Object
    (
        [d] => Sun
        [eid] => 2011103003
        [ga] => 
        [gsis] => 55268
        [gt] => REG
        [h] => CAR
        [hnn] => panthers
        [hs] => 21
        [q] => F
        [rz] => 0
        [t] => 1:00
        [v] => MIN
        [vnn] => vikings
        [vs] => 24
    )

[2] => stdClass Object
    (
        [d] => Sun
        [eid] => 2011103001
        [ga] => 
        [gsis] => 55269
        [gt] => REG
        [h] => HOU
        [hnn] => texans
        [hs] => 24
        [q] => F
        [rz] => 0
        [t] => 1:00
        [v] => JAC
        [vnn] => jaguars
        [vs] => 14
    )

[3] => stdClass Object
    (
        [d] => Sun
        [eid] => 2011103000
        [ga] => 
        [gsis] => 55270
        [gt] => REG
        [h] => NYG
        [hnn] => giants
        [hs] => 20
        [q] => F
        [rz] => 0
        [t] => 1:00
        [v] => MIA
        [vnn] => dolphins
        [vs] => 17
    )

[4] => stdClass Object
    (
        [d] => Sun
        [eid] => 2011103004
        [ga] => 
        [gsis] => 55271
        [gt] => REG
        [h] => STL
        [hnn] => rams
        [hs] => 31
        [q] => F
        [rz] => 0
        [t] => 1:00
        [v] => NO
        [vnn] => saints
        [vs] => 21
    )

[5] => stdClass Object
    (
        [d] => Sun
        [eid] => 2011103002
        [ga] => 
        [gsis] => 55272
        [gt] => REG
        [h] => TEN
        [hnn] => titans
        [hs] => 27
        [q] => F
        [rz] => 0
        [t] => 1:00
        [v] => IND
        [vnn] => colts
        [vs] => 10
    )

[6] => stdClass Object
    (
        [d] => Sun
        [eid] => 2011103007
        [ga] => 
        [gsis] => 55273
        [gt] => REG
        [h] => BUF
        [hnn] => bills
        [hs] => 23
        [q] => F
        [rz] => 0
        [t] => 4:05
        [v] => WAS
        [vnn] => redskins
        [vs] => 0
    )

[7] => stdClass Object
    (
        [d] => Sun
        [eid] => 2011103006
        [ga] => 
        [gsis] => 55274
        [gt] => REG
        [h] => DEN
        [hnn] => broncos
        [hs] => 10
        [q] => F
        [rz] => 0
        [t] => 4:05
        [v] => DET
        [vnn] => lions
        [vs] => 45
    )

[8] => stdClass Object
    (
        [d] => Sun
        [eid] => 2011103008
        [ga] => 
        [gsis] => 55275
        [gt] => REG
        [h] => PIT
        [hnn] => steelers
        [hs] => 25
        [q] => F
        [rz] => 0
        [t] => 4:15
        [v] => NE
        [vnn] => patriots
        [vs] => 17
    )

[9] => stdClass Object
    (
        [d] => Sun
        [eid] => 2011103010
        [ga] => 
        [gsis] => 55277
        [gt] => REG
        [h] => SEA
        [hnn] => seahawks
        [hs] => 12
        [q] => F
        [rz] => 0
        [t] => 4:15
        [v] => CIN
        [vnn] => bengals
        [vs] => 34
    )

[10] => stdClass Object
    (
        [d] => Sun
        [eid] => 2011103009
        [ga] => 
        [gsis] => 55276
        [gt] => REG
        [h] => SF
        [hnn] => 49ers
        [hs] => 20
        [q] => F
        [rz] => 0
        [t] => 4:15
        [v] => CLE
        [vnn] => browns
        [vs] => 10
    )

[11] => stdClass Object
    (
        [d] => Sun
        [eid] => 2011103011
        [ga] => 
        [gsis] => 55278
        [gt] => REG
        [h] => PHI
        [hnn] => eagles
        [hs] => 34
        [q] => F
        [rz] => 0
        [t] => 8:20
        [v] => DAL
        [vnn] => cowboys
        [vs] => 7
    )

[12] => stdClass Object
    (
        [d] => Mon
        [eid] => 2011103100
        [ga] => 
        [gsis] => 55279
        [gt] => REG
        [h] => KC
        [hnn] => chiefs
        [hs] => 0
        [q] => P
        [rz] => 0
        [t] => 8:30
        [v] => SD
        [vnn] => chargers
        [vs] => 0
    )

)

now如果我justed想要打印特定数组的信息。我该怎么做呢例如。

以上数组。我只需要数组中的信息[gsis] => 55279

1 个答案:

答案 0 :(得分:0)

你需要循环它们。发挥作用。

function search_object(array $object_array, $desired_number) {
    foreach($object_array as $one) {
        if($one->gsis == $desired_number) {
            return $one;
        }
    }
    //no result found
    return null;
}