Php 如何访问阵列/对象?
我有以下数组,当我执行Php 如何访问阵列/对象?,php,arrays,class,object,properties,Php,Arrays,Class,Object,Properties,我有以下数组,当我执行print_r(数组值($get_user)),我得到: Array ( [0] => 10499478683521864 [1] => 07/22/1983 [2] => email@saya.com [3] => Alan [4] => male [5] => Malmsteen [6] => https://
print_r(数组值($get_user))代码>,我得到:
Array (
[0] => 10499478683521864
[1] => 07/22/1983
[2] => email@saya.com
[3] => Alan [4] => male
[5] => Malmsteen
[6] => https://www.facebook.com app_scoped_user_id/1049213468352864/
[7] => stdClass Object (
[id] => 102173722491792
[name] => Jakarta, Indonesia
)
[8] => id_ID
[9] => El-nino
[10] => Alan El-nino Malmsteen
[11] => 7
[12] => 2015-05-28T04:09:50+0000
[13] => 1
)
我尝试按如下方式访问阵列:
echo $get_user[0];
但这让我明白:
未定义的0
注意:
我从Facebook SDK 4中获得了这个数组,所以我不知道原始数组结构
作为示例,我如何访问值email@saya.com
从数组?要访问数组
或对象
,您需要了解如何使用两个不同的运算符
要访问数组元素,您必须使用[]
或您看不太清楚但也可以使用的是{}
echo $array[0];
echo $array{0};
//Both are equivalent and interchangeable
声明数组和访问数组元素之间的区别
定义数组和访问数组元素是两件不同的事情。所以不要把它们混在一起
要定义数组,可以使用array()
或对于PHP>=5.4[]
并分配/设置数组/-元素。当您使用上面提到的[]
或{}
访问数组元素时,您会得到与设置元素相反的数组元素的值
//Declaring an array
$arrayA = array ( /*Some stuff in here*/ );
$arrayB = [ /*Some stuff in here*/ ]; //Only for PHP >=5.4
//Accessing an array element
echo $array[0];
echo $array{0};
因此,请注意您使用的键表达式以及PHP如何解释它:
echo $array[0]; //The key is an integer; It accesses the 0's element
echo $array["0"]; //The key is a string; It accesses the 0's element
echo $array["string"]; //The key is a string; It accesses the element with the key 'string'
echo $array[CONSTANT]; //The key is a constant and it gets replaced with the corresponding value
echo $array[cOnStAnT]; //The key is also a constant and not a string
echo $array[$anyVariable] //The key is a variable and it gets replaced with the value which is in '$anyVariable'
echo $array[functionXY()]; //The key will be the return value of the function
要访问对象属性,必须使用->
echo $object->property;
注意:
如果你有一个无效的属性名,你也必须小心!因此,要查看使用无效属性名时可能遇到的所有问题,请参见以下内容。尤其是如果在属性名称的开头有数字
只能从类外部访问具有public的属性。否则(私有或受保护)您需要一个方法或反射,您可以使用它来获取属性的值
echo $objectA->objectB->property;
数组和对象
现在,如果数组和对象相互混合,只需查看是否访问数组元素或对象属性,并使用相应的运算符即可
//Object
echo $object->anotherObject->propertyArray["elementOneWithAnObject"]->property;
//├────┘ ├───────────┘ ├───────────┘ ├──────────────────────┘ ├──────┘
//│ │ │ │ └── property ;
//│ │ │ └───────────────────────────── array element (object) ; Use -> To access the property 'property'
//│ │ └─────────────────────────────────────────── array (property) ; Use [] To access the array element 'elementOneWithAnObject'
//│ └────────────────────────────────────────────────────────── property (object) ; Use -> To access the property 'propertyArray'
//└────────────────────────────────────────────────────────────────── object ; Use -> To access the property 'anotherObject'
//Array
echo $array["arrayElement"]["anotherElement"]->object->property["element"];
//├───┘ ├────────────┘ ├──────────────┘ ├────┘ ├──────┘ ├───────┘
//│ │ │ │ │ └── array element ;
//│ │ │ │ └─────────── property (array) ; Use [] To access the array element 'element'
//│ │ │ └─────────────────── property (object) ; Use -> To access the property 'property'
//│ │ └────────────────────────────────────── array element (object) ; Use -> To access the property 'object'
//│ └────────────────────────────────────────────────────── array element (array) ; Use [] To access the array element 'anotherElement'
//└──────────────────────────────────────────────────────────── array ; Use [] To access the array element 'arrayElement'
数组、对象和循环
如果不想只访问单个元素,可以在嵌套数组/对象上循环,并遍历特定维度的值
为此,您只需访问要循环的维度,然后就可以循环该维度的所有值
我们以数组为例,但它也可以是对象:
Array (
[data] => Array (
[0] => stdClass Object (
[propertyXY] => 1
)
[1] => stdClass Object (
[propertyXY] => 2
)
[2] => stdClass Object (
[propertyXY] => 3
)
)
)
如果在第一个维度上循环,将获得第一个维度的所有值:
foreach($array as $key => $value)
foreach($array["data"] as $key => $value)
如果在第二个维度上循环,将获得第二个维度的所有值:
foreach($array as $key => $value)
foreach($array["data"] as $key => $value)
通过这个,你可以循环通过任何你想要的维度,不管它是数组还是对象
分析//输出
所有这3个调试函数都输出相同的数据,只是以另一种格式或带有一些元数据(例如类型、大小)。因此,这里我想展示如何读取这些函数的输出,以了解/了解如何从数组/对象访问某些数据
输入阵列:
$array = [
"key" => (object) [
"property" => [1,2,3]
]
];
var\u dump()
输出:
array(1) {
["key"]=>
object(stdClass)#1 (1) {
["property"]=>
array(3) {
[0]=>
int(1)
[1]=>
int(2)
[2]=>
int(3)
}
}
}
Array
(
[key] => stdClass Object
(
[property] => Array
(
[0] => 1
[1] => 2
[2] => 3
)
)
)
array (
'key' =>
stdClass::__set_state(array(
'property' =>
array (
0 => 1,
1 => 2,
2 => 3,
),
)),
)
print\u r()
输出:
array(1) {
["key"]=>
object(stdClass)#1 (1) {
["property"]=>
array(3) {
[0]=>
int(1)
[1]=>
int(2)
[2]=>
int(3)
}
}
}
Array
(
[key] => stdClass Object
(
[property] => Array
(
[0] => 1
[1] => 2
[2] => 3
)
)
)
array (
'key' =>
stdClass::__set_state(array(
'property' =>
array (
0 => 1,
1 => 2,
2 => 3,
),
)),
)
var\u export()
输出:
array(1) {
["key"]=>
object(stdClass)#1 (1) {
["property"]=>
array(3) {
[0]=>
int(1)
[1]=>
int(2)
[2]=>
int(3)
}
}
}
Array
(
[key] => stdClass Object
(
[property] => Array
(
[0] => 1
[1] => 2
[2] => 3
)
)
)
array (
'key' =>
stdClass::__set_state(array(
'property' =>
array (
0 => 1,
1 => 2,
2 => 3,
),
)),
)
正如你所看到的,所有的输出都非常相似。如果你现在想访问值2,你可以从值本身开始,你想访问它,然后找到“左上角”
一,。我们首先看到,值2位于一个带有键1的数组中
array(3) { //var_dump()
[0]=>
int(1)
[1]=>
int(2)
[2]=>
int(3)
}
输出将是:
2
不要让PHP欺骗你!
有几件事你必须知道,这样你就不用花很多时间去寻找它们了
“隐藏”字符
有时,关键帧中有字符,但在浏览器中第一次查看时看不到这些字符。然后你会问自己,为什么你不能访问元素。这些字符可以是:制表符(\t
)、新行(\n
)、空格或html标记(例如
,
)等
例如,如果查看print\r()
的输出,您会看到:
Array ( [key] => HERE )
然后,您尝试使用以下命令访问元素:
echo $arr["key"];
但是你得到了通知:
注意:未定义索引:key
这是一个很好的迹象,表明一定有一些隐藏字符,因为即使键看起来非常正确,您也无法访问元素
这里的技巧是使用var\u dump()
+查看您的源代码!(可选:突出显示字符串(打印($variable,TRUE));
)
突然间你可能会看到这样的东西:
array(1) {
["</b>
key"]=>
string(4) "HERE"
}
您将获得所需的输出:
HERE
如果查看XML,请不要信任print\u r()
或var\u dump()
的输出
您可以将XML文件或字符串加载到对象中,例如
<?xml version="1.0" encoding="UTF-8" ?>
<rss>
<item>
<title attribute="xy" ab="xy">test</title>
</item>
</rss>
正如你所看到的,你看不到标题的属性。因此,正如我所说,当您有一个XML对象时,千万不要相信var\u dump()
或print\r()
的输出。始终使用以查看完整的XML文件/字符串
因此,只需使用以下方法之一:
echo $xml->asXML(); //And look into the source code
highlight_string($xml->asXML());
header ("Content-Type:text/xml");
echo $xml->asXML();
然后您将获得输出:
<?xml version="1.0" encoding="UTF-8"?>
<rss>
<item>
<title attribute="xy" ab="xy">test</title>
</item>
</rss>
试验
有关更多信息,请参阅:
常规(符号、错误)
属性名称问题
从问题中我们看不到输入数组的结构。它可能是数组('id'=>10499478683521864,'date'=>'07/22/1983')
。因此,当您询问$demo[0]时,您使用未查找索引
Array_为丢失的键赋值并返回包含多个键的数组,使数组成为数组(10499478683521864,'07/22/1983'…)
。我们在问题中看到了这个结果
因此,可以用相同的方法获取数组项的值
echo array_values($get_user)[0]; // 10499478683521864
如果您的打印($var)
输出为:
Array ( [demo] => Array ( [0] => 10499478683521864 [1] => 07/22/1983 [2] => email@saya.com ) )
Array ( [0] => 10499478683521864 [1] => 07/22/1983 [2] => email@saya.com )
然后执行$var['demo'][0]
如果打印($var)的输出为:
Array ( [demo] => Array ( [0] => 10499478683521864 [1] => 07/22/1983 [2] => email@saya.com ) )
Array ( [0] => 10499478683521864 [1] => 07/22/1983 [2] => email@saya.com )
然后执行$var
SimpleXMLElement Object
(
[item] => SimpleXMLElement Object
(
[title] => test
)
)
echo $xml->asXML(); //And look into the source code
highlight_string($xml->asXML());
header ("Content-Type:text/xml");
echo $xml->asXML();
<?xml version="1.0" encoding="UTF-8"?>
<rss>
<item>
<title attribute="xy" ab="xy">test</title>
</item>
</rss>
echo array_values($get_user)[0]; // 10499478683521864
Array ( [demo] => Array ( [0] => 10499478683521864 [1] => 07/22/1983 [2] => email@saya.com ) )
Array ( [0] => 10499478683521864 [1] => 07/22/1983 [2] => email@saya.com )
$ar = (array) $get_user;
echo $ar[0];