【问题标题】:Can't get single value from this php array无法从此 php 数组中获取单个值
【发布时间】:2017-09-22 10:18:00
【问题描述】:
var_dump($locations);

显示:

array(1) { 
    [0]=> object(WP_Term)#3193 (10) { 
        ["term_id"]=> int(11) 
        ["name"]=> string(15) "Agence Himalaya" 
        ["slug"]=> string(15) "agence-himalaya" 
        ["term_group"]=> int(0) 
        ["term_taxonomy_id"]=> int(11) 
        ["taxonomy"]=> string(8) "location" 
        ["description"]=> string(0) "" 
        ["parent"]=> int(0) 
        ["count"]=> int(1) 
        ["filter"]=> string(3) "raw" 
    } 
}

我需要在这个数组中获取["name"]

我尝试了$locations[0]["name"] 和不同的东西,但我没有设法获得物品的价值。

雨果

【问题讨论】:

  • $locations[0]->name?
  • 谢谢,它成功了,我得到了字符串(15)“Agence Himalaya”。有没有办法只获取字符串?
  • @Hugo M.,请把你的数组像 print_r($locations) 放在你的问题中......
  • 因为你使用了vardump。它也会打印结果的类型。只需使用echo只打印字符串
  • @amow 你说得对,作为初学者我不知道 :)

标签: php arrays wordpress


【解决方案1】:

WP_Term 是一个对象,如果它没有实现ArrayAccess,你不能使用$obj['name'] 来读取它的name 属性。

如果是WP_Term$name 是一个公共变量,所以你可以使用$obj->name

在你的情况下,解决方案是

$name = $locations[0]->name;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多