dongshuohuan5291 2012-11-08 23:50
浏览 49
已采纳

从多维数组中获取特定值[重复]

Possible Duplicate:
Able to see a variable in print_r()'s output, but not sure how to access it in code

stdClass Object ( [geonames] => Array ( [0] => stdClass Object ( [countryName] => United States [adminCode1] => TX [fclName] => city, village,... [countryCode] => US [lng] => -97.3455673 [fcodeName] => populated place [distance] => 1.83381 [toponymName] => Greenock [fcl] => P [name] => Greenock [fcode] => PPL [geonameId] => 4695037 [lat] => 31.7662717 [adminName1] => Texas [population] => 0 ) ) )

this is the result I got from a print_r($results). I need to get [name] and [adminname1] values. I have tried several ways and I just keep getting array{ or array as a result.. any help please

EDIT: here is a var_dump:

object(stdClass)#1 (1) { ["geonames"]=> array(1) { [0]=> object(stdClass)#2 (15) { ["countryName"]=> string(13) "United States" ["adminCode1"]=> string(2) "TX" ["fclName"]=> string(17) "city, village,..." ["countryCode"]=> string(2) "US" ["lng"]=> float(-97.3455673) ["fcodeName"]=> string(15) "populated place" ["distance"]=> string(7) "1.83381" ["toponymName"]=> string(8) "Greenock" ["fcl"]=> string(1) "P" ["name"]=> string(8) "Greenock" ["fcode"]=> string(3) "PPL" ["geonameId"]=> int(4695037) ["lat"]=> float(31.7662717) ["adminName1"]=> string(5) "Texas" ["population"]=> int(0) } } }
  • 写回答

1条回答 默认 最新

  • dongyu9894 2012-11-09 00:00
    关注

    I think this will do it:

    $name = $object->geonames[0]->name;
    $adminname1 = $object->geonames[0]->adminName1;
    

    If this isn't working, break it up into multiple assignments to see where it's going wrong:

    $thing1 = $object->geonames;
    $thing2 = $thing1[0];
    $name = $thing2->name;
    

    At which step does this get an error?

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 labview程序设计
  • ¥15 为什么在配置Linux系统的时候执行脚本总是出现E: Failed to fetch http:L/cn.archive.ubuntu.com
  • ¥15 Cloudreve保存用户组存储空间大小时报错
  • ¥15 伪标签为什么不能作为弱监督语义分割的结果?
  • ¥15 编一个判断一个区间范围内的数字的个位数的立方和是否等于其本身的程序在输入第1组数据后卡住了(语言-c语言)
  • ¥15 游戏盾如何溯源服务器真实ip?
  • ¥15 Mac版Fiddler Everywhere4.0.1提示强制更新
  • ¥15 android 集成sentry上报时报错。
  • ¥50 win10链接MySQL
  • ¥15 抖音看过的视频,缓存在哪个文件